Java 扫描仪变量未注册为字符串
我正在制作一个java计算器来计算蔗糖水溶液(化学物质)的密度和粘度,并决定用它做得更进一步。我使用Micheal Flanagan的蔗糖Java库,它是:Java 扫描仪变量未注册为字符串,java,string,java.util.scanner,Java,String,Java.util.scanner,我正在制作一个java计算器来计算蔗糖水溶液(化学物质)的密度和粘度,并决定用它做得更进一步。我使用Micheal Flanagan的蔗糖Java库,它是: import flanagan.physprop.Sucrose; 在代码中。我想找到一种方法,不必继续一遍又一遍地按run键就可以继续输入方程,所以我创建了 public static void restart() { System.out.println("would you like to calculate another
import flanagan.physprop.Sucrose;
在代码中。我想找到一种方法,不必继续一遍又一遍地按run键就可以继续输入方程,所以我创建了
public static void restart() {
System.out.println("would you like to calculate another solution?(y/n)");
String yn = new String(input.nextLine());
String y = "y";
String n = "n";
System.out.println(yn);
if (yn == y){
tf = true;
}
else {
if (yn == n) {
tf = false;
}
else {
System.out.println("ERROR: Please print 'y' or 'n'");
restart();
}
}
if (tf == true) {
calc();
}
if (tf == false) {
fin();
}
}
我正在使用一个名为“导入”的扫描仪,它在前面的一行中定义
static Scanner input = new Scanner(System.in);
所以,不管怎样,代码不是重新启动,而是触发错误。我尝试通过将读出定义为字符串并为“y”和“n”创建变量来修复它。我认为“yn”没有正确地注册为字符串,并且不能想到其他任何东西
可以找到flanagan.jar。嗯,我通过切换到数字得到了答案
public static void restart() {
System.out.println("would you like to calculate another solution?(1 for yes/0 for no)");
int yn = input.nextInt();
int y = 1;
int n = 0;
if (yn == y){
calculator();
}
if (yn == n) {
fin();
}
}
你需要做的就是使用一个无限循环:
while(true) {
//code you want to repeat...
}
扫描仪是否有
nextSymbol()
方法?我看了看报纸,没找到。这段代码甚至用这个方法编译吗?事实上,整个语句都很奇怪:newstring(input.nextSymbol())
。为什么要对任何内容调用新字符串(…)
。什么叫restart()方法?你到底得到了什么错误(例外)?气垫船,我知道它看起来很奇怪,因为这是我最后的尝试。现在我正在使用eclipse,它甚至不能编译!yn==y&!yn==n
应替换双else。