我的代码是一个非常基本的程序,而不是我输入System.out.println时,我得到了错误:"对println的引用含糊不清"并且随机出现它用来工作并且没有错误,但现在它确实发生了这个错误,所以我怎么解决这个问题,而不是你。
package blue.light;
//name is: slash
import java.util.*;
public class BlueLight {
public static void main(String[] args) throws InterruptedException {
Scanner scan = new Scanner(System.in);
String name;
int age;
String a;
String yes;
String no;
double num1;
double num2;
double ans;
System.out.println("hello, my name is Slash");
System.out.println("What is your name?");
name = scan.nextLine();
System.out.println("Hello " + name);
Thread.sleep(1000);
System.out.println("How old are you?");
age = scan.nextInt();
if(age < 18)
{
System.out.println("ur young af");
}
else if (age == 21)
{
System.out.println("Shots, shots, shots");
}
else if (age == 69)
{
System.out.println("ohhh yea");
}
System.out.println("you to calculate any thing?");
a = scan.nextLine();
if(a==("yes")){
System.out.println("What would u like to do?(multiply,or add)");
a = scan.nextLine();
if(a == "add"){
System.out.println("Enter number 1");
num1 = scan.nextDouble();
System.out.println("Enter number 2");
num2 = scan.nextDouble();
ans = num1+num2;
}
}
}
}
答案 0 :(得分:0)
首先,nextInt()
在年龄之后不会消耗新行(因此您的nextLine
为空)。您将Object
值与.equals()
调用进行比较,因为==
测试引用标识(并使用String
我使用String.equalsIgnoreCase(String)
)。最后,我将在Java中声明具有最小可见性的变量;尽可能接近他们的第一次使用。像,
public static void main(String[] args) throws InterruptedException {
Scanner scan = new Scanner(System.in);
System.out.println("hello, my name is Slash");
System.out.println("What is your name?");
String name = scan.nextLine();
System.out.println("Hello " + name);
Thread.sleep(1000);
System.out.println("How old are you?");
int age = scan.nextInt();
if (age < 18) {
System.out.println("ur young af");
} else if (age == 21) {
System.out.println("Shots, shots, shots");
} else if (age == 69) {
System.out.println("ohhh yea");
}
System.out.println("you to calculate any thing?");
scan.nextLine(); // <-- nextInt leaves a trailing new line.
String a = scan.nextLine();
if (a.equalsIgnoreCase("yes")) { // <-- compare string for equality in
// Java
System.out.println("What would u like to do?(multiply,or add)");
a = scan.nextLine();
if (a.equalsIgnoreCase("add")) {// <-- compare string for equality
// in Java
System.out.println("Enter number 1");
double num1 = scan.nextDouble();
System.out.println("Enter number 2");
double num2 = scan.nextDouble();
double ans = num1 + num2;
System.out.println(ans); // <-- display answer
}
}
}