从字面上看,今天开始使用Java,而我的教授已经给我班上了修改一些非常基本的代码的任务。
如果n1和n2的总和为666,我想修改代码以使其显示一条消息,但是我不希望它打印实际的总和或通常附加到其上的消息。我在这里周围的某个地方看到了类似的问题,但该解决方案似乎对我不起作用。我不知道为什么。请帮忙。
import java.io.Console;
import java.util.Scanner;
public class FirstProgram{
Console t = new Console();
public static void main(String[] args)
{
System.out.println("Hello out there.");
System.out.println("I will add two numbers for you.");
System.out.println("Enter two whole numbers on a line:");
int n1, n2;
Scanner keyboard = new Scanner(System.in);
n1 = keyboard.nextInt( );
n2 = keyboard.nextInt( );
//This should print normally when the sum is anything BUT 666
System.out.println("The sum of those two numbers is");
System.out.println(n1 + n2);
//If the sum IS 666, I don't want it to print the above lines, just the one below.
if (n1 + n2 == 666);
t.println("Nice try, Satan");
}
}
它给出了两个主要错误:构造函数Console()不可见,并且我无法对非静态字段t进行静态引用。我不知道这意味着什么或如何解决。
答案 0 :(得分:1)
您应该学习如何进行条件语句。如果您不告诉Java如何做到这一点,Java不会“忽略”并传递给另一件事。记住:如果计算机不告诉用户要执行的操作以及如何执行操作,计算机将无法执行任何操作。
您不是要初始化n1和n2,应在从输入中获取值后对其进行初始化。
并且如注释中所述,请始终在大括号{}中包装循环和条件语句,以确保将要执行的代码是大括号内的代码。
import java.util.Scanner;
public class FirstProgramm{
public static void main(String[] args){
System.out.println("Hello out there.");
System.out.println("I will add two numbers for you.");
System.out.println("Enter two whole numbers on a line:");
Scanner keyboard = new Scanner(System.in);
int n1 = keyboard.nextInt( );
int n2 = keyboard.nextInt( );
//See? the result is stored inside this variable
int sum = n1 + n2;
//If the sum is equal 666 then print the message
if(sum == 666) {
System.out.println("Nice try, Satan");
}else {
//Else if the sum is something else, print it
System.out.println("The sum of those two numbers is");
System.out.println(sum);
}
}
}
您甚至可以与if用来评估条件的运算符一起玩:
if(sum != 666) { //If sum is `not equal to` 666... if the sum is anything else than 666, print it
System.out.println("The sum of those two numbers is");
System.out.println(sum);
}else {// But if it is 666, print what is inside the parentheses
System.out.println("Nice try, Satan");
}
答案 1 :(得分:1)
我会在这里帮助您。
首先: the constructor Console() is not visible
我认为这是指Console
并不是真的应该被这样访问的事实。 Console
的构造函数是private
,这意味着外部类无法访问它。要解决此问题,当您想打印到控制台时,请使用System.console
。
其次: I cannot make a static reference to a non-static field t
这个新人很难解释。您的main
函数是static
,这意味着无需实例化包含它的类就可以对其进行访问。您的变量t
是一个实例变量,这意味着在初始化类后,该类中的每个函数都可以访问它。但是,由于main
函数是static
,因此您无法访问非静态变量,因为它可能尚未初始化。如果要访问static
函数中的实例变量,则还需要使该变量static
成为一个类变量,该变量始终可以访问。
最后
要使代码正常工作,您需要阅读if
语句。这是一个条件查询,基本上是在询问if this statement is true, do this
。也有else if
和else
的语句。
正确的else if this statement is true, do this
/ else do this
/ if
语句示例:
else if
因此,要修复代码,您需要执行以下操作:
else
答案 2 :(得分:0)
我为您重新编写了代码,并提供了一些建议以实现您的需求。
import java.util.Scanner;
public class FirstProgram {
// I have removed the Console variable, you don't need that.
// System.out.println prints to the console.
// Use constants for any number or string used to give them meaning
private static final int DEVILS_NUMBER = 666;
public static void main(String[] args) {
System.out.println("Hello out there.");
System.out.println("I will add two numbers for you.");
System.out.println("Enter two whole numbers on a line:");
Scanner keyboard = new Scanner(System.in);
// declare variables next to where they are used.
// additionally, never declare more than one variable per line.
// never do this: int n1, n2;
int n1 = keyboard.nextInt();
int n2 = keyboard.nextInt();
// store the sum in a variable so you can refer to it without doing the sum many times
int sum = n1 + n2;
//If the sum IS DEVILS_NUMBER, I don't want it to print the above lines, just the one below.
// always test the positive possibility first, never the negation
if (DEVILS_NUMBER == sum) {
System.out.println("Nice try, Satan");
} else {
//This should print normally when the sum is anything BUT DEVILS_NUMBER
System.out.println("The sum of those two numbers is");
System.out.println(n1 + n2);
}
}
最后但并非最不重要的一点,请查看ranges,以获取有关如何正确格式化代码的提示。如果您使用的是Eclipse,Intellij或NetBeans之类的IDE,它将自动为您设置代码格式。