【发布时间】:2014-09-12 01:10:14
【问题描述】:
你好,所以我正在尝试编写一个测试运算符的代码,并且需要一些帮助来声明变量。 因此,在我的 if 语句下的代码中,我根据输入的数字将某个答案声明为真或假,并且我一直陷入“找不到符号”错误。这是我的代码
import java.util.Scanner;
public class TestOperators
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//prompt the user to enter an integer
System.out.print("Enter an integer: ");
int num = input.nextInt();
String t = new String("True");
String f = new String("False");
//calculate
if ((num % 5 == 0) && (num % 6 == 0))
answer = t;
else if ((num % 5 == 0) && (num % 6 != 0) || (num % 5 != 0) && (num % 6 == 0))
answer = t;
else if ((num % 5 == 0) || (num % 6 == 0))
answer = t;
else
answer = f;
//print results
System.out.println("Is " + num + " divisible by 5 and 6? " + answer);
System.out.println("Is " + num + " divisible by 5 or 6?" + answer);
System.out.print("Is " + num + " divisible by 5 or 6, but not both?" + answer);
}
}
程序告诉我不能在那个地方声明变量“answer”。我试图将字符串“f”和“t”变量与答案变量联系起来,但不知道如何。请帮忙!
【问题讨论】:
标签: java string variables if-statement java.util.scanner