gpt4 book ai didi

java - *此处插入单词*无法解析为变量

转载 作者:行者123 更新时间:2023-12-01 18:44:47 25 4
gpt4 key购买 nike

我正在为学校做一个项目,似乎找不到此错误的原因。我对编程非常陌生,感谢您的帮助。预先感谢您。

import java.util.Scanner;
public class Lemonade {

public static void main(String[] args) {
Scanner user = new Scanner(System.in);
int lemons_per_pitcher = 12;
int spoons_per_bag = 1000;
int spoons_per_pitcher = 50;
System.out.println("Enter the amount of lemons you have.");
int lemon = user.nextInt();
System.out.println("Enter the amount of bags of sugar you have.");
int bags = user.nextInt();
int spoons = bags * 1000;
int sugar = spoons / 50;
int lemons2 = lemon / 12;
if( lemons2 > sugar){
int pitcher = lemons2;
}else{
int pitcher = sugar;
}
if( lemon < 12 || bags < 1){
System.out.println("You can make a maximum of 0 pitchers");
} else{
System.out.println("This is the maximum amount of pitchers you can make is: " + pitcher);
}
}

}

最佳答案

pitcher 是一个本地值,因此您可以在 main 方法中定义它。

试试这个:

public static void main(String[] args) {
int pitcher;
Scanner user = new Scanner(System.in);
int lemons_per_pitcher = 12;
int spoons_per_bag = 1000;
int spoons_per_pitcher = 50;
System.out.println("Enter the amount of lemons you have.");
int lemon = user.nextInt();
System.out.println("Enter the amount of bags of sugar you have.");
int bags = user.nextInt();
int spoons = bags * 1000;
int sugar = spoons / 50;
int lemons2 = lemon / 12;
if (lemons2 > sugar) {
pitcher = lemons2;
} else {
pitcher = sugar;
}
if (lemon < 12 || bags < 1) {
System.out.println("You can make a maximum of 0 pitchers");
} else {
System.out
.println("This is the maximum amount of pitchers you can make is: "
+ pitcher);
}
}

您只能在定义变量的 block 中使用变量。

例如:

{
int i = 0;
}
i++; // ERROR : There no i in this block

在您的代码中:

if( lemons2 > sugar){
int pitcher = lemons2;
}else{
int pitcher = sugar;
} // pitcher no more exists after block

关于java - *此处插入单词*无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18262563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com