2016-08-22 5 views
1
aufgelöst werden kann
import java.util.Scanner; 
public class HowManyToKeep { 

    public static void main(String[] args) { 
     int currentcandies; 
     int currentamountofpokemon; 
     int howmanycandiestoevolve; 
     int keepamount; 
     int x =0; 

     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter how many candies you have: "); 
     currentcandies = sc.nextInt(); 
     System.out.println("Enter how many pokemon you have: "); 
     currentamountofpokemon = sc.nextInt(); 
     System.out.println("Enter how many candies to evolve: "); 
     howmanycandiestoevolve = sc.nextInt(); 
     sc.close(); 

     for (int i=0;i<100;i++) { 
     int grosstotalcandies = currentcandies + currentamountofpokemon; 
     int howmanycanevolve = grosstotalcandies/howmanycandiestoevolve; 
     int totalamountofcandies = howmanycanevolve + grosstotalcandies; 
     } 

     keepamount = totalamountofcandies/howmanycandiestoevolve; 
     System.out.println("The total amount you should keep is: " + keepamount); 

    } 


} 

Also meine Frage ist, warum es nicht sagen, dass die Variable „totalamountofcandies“ nicht beheben kann, wenn ich es in der Schleife definiert ? Wie soll ich es zur Arbeit bringenJava: Warum kann dieser Code Arbeit, sagt es, dass mein Variable nicht

+1

weil es im Bereich der for-Schleife definiert ist und daher nicht mehr existiert. – SomeJavaGuy

+0

Das Ziel dieses Codes ist es, automatisch die maximale Menge an Pokemon zu berechnen, die man in Pokemon entwickeln kann, wenn man eine große Menge Pokemon und Bonbons desselben Typs hat. –

+1

Hinweis: Lesen Sie einige Java Code Style Guide Lines. Sie verwenden camelCase für Variablennamen, und überraschenderweise wird growTotalCandies plötzlich für Menschen lesbar! – GhostCat

Antwort

4

Der Bereich totalamountofcandies ist nur innerhalb der Schleife. Um außerhalb der Schleife darauf zuzugreifen, müssen Sie es vor der Schleife deklarieren.

Abgesehen davon, ich gehe davon aus, dass Sie in der Schleife gesammelt alle Werte hinzufügen möchten, und nicht den Wert von totalamountofcandies in jeder Iteration überschreiben:

int totalamountofcandies = 0; 
for (int i=0;i<100;i++) { 
    int grosstotalcandies = currentcandies + currentamountofpokemon; 
    int howmanycanevolve = grosstotalcandies/howmanycandiestoevolve; 
    totalamountofcandies += howmanycanevolve + grosstotalcandies; 
} 
-2

Sie müssen die Variable global machen, die Der Gültigkeitsbereich dieser Variablen befindet sich innerhalb der geschweiften Klammern

+0

Nicht unbedingt * global *. – Biffen

+0

Und für einen Anfänger, was denkst du wird er über "globale" Variablen verstehen. Sie setzen ihn wirklich auf die völlig falsche Spur – GhostCat

+0

er muss das lernen, und ich glaube nicht, dass er sein ganzes Leben leben wird, ohne dies zu wissen, es ist gut, etwas neues etwas Nützliches zu wissen :) –

0

Die Variable wird in for-Schleife deklariert (der Bereich liegt innerhalb dieser for-Schleife). Sie versuchen, außerhalb für Schleife auf es zuzugreifen. An dieser Stelle steht die Variable dem Java-Compiler nicht zur Verfügung. Es wirft also einen Fehler auf. Dies sollte für Sie funktionieren.

int totalamountofcandies = 0; 
for (int i=0;i<100;i++) { 
int grosstotalcandies = currentcandies + currentamountofpokemon; 
int howmanycanevolve = grosstotalcandies/howmanycandiestoevolve; 
totalamountofcandies += howmanycanevolve + grosstotalcandies; 
} 

Jetzt können Sie überall innerhalb dieses Programms Zugriff auf Totalanmountofcandies.

Verwandte Themen