java - Problems assigning variable to the return of a function -
java - Problems assigning variable to the return of a function -
i have been searching online solutions problem hours, cannot find one. beginner java don't understand many of errors , why wasn't able right myself.
anyway, trying assign value variable , add together int value variable another. when compiling getting errors: "this not statement" , error missing semicolon suspect cleared if prepare statement.
here error, in case need it:
hw8_ex2_jm2rv.java:61: error: ';' expected int sumofgames += value; ^ hw8_ex2_jm2rv.java:61: error: not statement int sumofgames += value; ^ 2 errors
this code:
public static int onegame() { int truevalue = 1; int falsevalue = 0; int total = sumtwodice(); if((total == 7) || (total == 11)) { //system.out.println(truevalue + " " + total); return(truevalue); } else if((total == 2) || (total == 3) || (total == 12)) { //system.out.println(falsevalue + " " + total); return(falsevalue); } else { int total2 = 0; while((total2 != 7) || (total2 != total)) { total2 = sumtwodice(); if(total2 == total) { //system.out.println(truevalue + " " + total + " " + total2); return(truevalue); } else if(total2 == 7) { //system.out.println(falsevalue + " " + total + " " + total2); return(falsevalue); } } } }
you wondering sumtwodice method, homecoming number between 2 , 12 , know working.if want see can post too, ask.
the onegame function simulates game of craps , returns integer value 1 or 0. 1 beingness win , 0 beingness loss.
//this method runs simulation of n values , returns avg. public static double montecarlosim() { int truevalue = 1; int falsevalue = 0; int sumofgames = 0; int n = 2; (int = 0; <= n; i++) { int value = onegame(); int sumofgames += value; } double avg = sumofgames / n; return(avg); }
i have little n @ moment easier deal , faster compile.
my main question makes statement invalid , can do prepare it, since many of threads don't cover on stack overflow, makes a statement invalid in general. help both me , other beginners don't understand error.
thanks in advance help , knowledge!
declare sum variable outside loop:
int sumofgames = 0;
the increment value within loop:
int sumofgames = 0; (int = 0; <= n; i++) { int value = onegame(); sumofgames += value; }
java
Comments
Post a Comment