Java:为什么可以';如果这段代码不起作用,它表示我的变量无法解析 import java.util.Scanner; 公共课多少钱{ 公共静态void main(字符串[]args){ 糖果; int currentAmountOffokeMon; int有多少人会进化; 内特基帕蒙特; int x=0; 扫描仪sc=新的扫描仪(System.in); System.out.println(“输入您有多少糖果:”); currentcandies=sc.nextInt(); println(“输入你有多少口袋妖怪:”); CurrentAmountOffokeMon=sc.nextInt(); println(“输入要进化的糖果数量:”; howmanycandiestoevolve=sc.nextInt(); sc.close(); 对于(int i=0;i

Java:为什么可以';如果这段代码不起作用,它表示我的变量无法解析 import java.util.Scanner; 公共课多少钱{ 公共静态void main(字符串[]args){ 糖果; int currentAmountOffokeMon; int有多少人会进化; 内特基帕蒙特; int x=0; 扫描仪sc=新的扫描仪(System.in); System.out.println(“输入您有多少糖果:”); currentcandies=sc.nextInt(); println(“输入你有多少口袋妖怪:”); CurrentAmountOffokeMon=sc.nextInt(); println(“输入要进化的糖果数量:”; howmanycandiestoevolve=sc.nextInt(); sc.close(); 对于(int i=0;i,java,Java,而言,totalamountofcandies的范围仅在循环内。要在循环外访问它,必须在循环前声明它 除此之外,我假设您希望添加循环中收集的所有值,而不是在每次迭代中覆盖totalamountofcandies的值: import java.util.Scanner; public class HowManyToKeep { public static void main(String[] args) { int currentcandies; int

而言,
totalamountofcandies
的范围仅在循环内。要在循环外访问它,必须在循环前声明它

除此之外,我假设您希望添加循环中收集的所有值,而不是在每次迭代中覆盖
totalamountofcandies
的值:

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);

    }


}
int totalamountofcandies=0;

对于(int i=0;i必须使变量为全局变量,该变量的范围在两个大括号内,

变量在for循环中声明(范围在for循环中)。您正试图在for loop外部访问它。此时,java编译器无法使用该变量。因此,它会引发一个错误。这应该适用于您

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

对于(int i=0;i因为它是在
for
循环的范围内定义的,因此在它之后就不存在了。此代码的目的是自动计算当有大量的口袋妖怪和相同类型的糖果时,你可以在口袋妖怪go中进化的口袋妖怪的最大数量提示:阅读一些java代码风格的指导行。你使用camelCase的变量名,令人惊讶的是,grossTotalCandies突然变成了人类可读的东西!谢谢大家,我已经得到了我需要的东西,很抱歉我在Java中的愚蠢,对这一切都很陌生,不一定是全局的。对于一个初学者,你认为他会理解“全局的”吗变数。你真的让他走上了一条完全错误的道路。他需要学习这一点。我不认为他会一辈子都不知道这一点,知道一些新的有用的东西是很好的:)
int totalamountofcandies = 0;
for (int i=0;i<100;i++) {
int grosstotalcandies = currentcandies + currentamountofpokemon;
int howmanycanevolve = grosstotalcandies / howmanycandiestoevolve;
totalamountofcandies += howmanycanevolve + grosstotalcandies;
}