import java.util.Scanner;
public class program4
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println(" Hello and welcome to the program ");
System.out.println("Enter your number.");
int number = input.nextInt();
int integerDoubled;
System.out.println("Your result is" + doubleNumber(number));
System.out.println(" thank you for using my program ");
}
public static int doubleNumber (int x){
int integerDoubled;
return integerDoubled = (x*2);
}
if (integerDoubled < 100 ); {
less_100(integerDoubled);
} else if ;
greater_100(integerDoubled);
public static int less_100 (int integerDoubled)
{
int integerDoubled;
return integerDoubled =(x*2);
}
public static int greater_100 (int integerDoubled)
{
int integerDoubled;
return integerDoubled =(x*3);
}
}
}
我认为错误发生在If语句中,但我不知道如何修复它。 我知道integerDoubled是一个局部变量,我应该把它变成全局但是我不知道怎么做。我应该将if语句与doubleNumber函数一起使用吗?
答案 0 :(得分:0)
如果您格式化了代码(您的IDE将为您执行此操作),您会发现if
语句不在必须的方法内。如果你正确使用你的IDE,找到并纠正这样的错误会更快,你几乎不必考虑它。
答案 1 :(得分:0)
我试图了解你的程序,这就是我想出来的
import java.util.Scanner;
public class T
{
public static int doubleNumber (int x){
int integerDoubled;
return integerDoubled = (x*2);
}
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println(" Hello and welcome to the program ");
System.out.println("Enter your number.");
int number = input.nextInt();
int integerDoubled=doubleNumber(number);
System.out.println("Your result is" + integerDoubled);
System.out.println(" thank you for using my program ");
if (integerDoubled < 100 ) {
less_100(integerDoubled);
}
else
greater_100(integerDoubled);
}
public static int less_100 (int integerDoubled)
{
return integerDoubled =(integerDoubled*2);
}
public static int greater_100 (int integerDoubled)
{
return integerDoubled =(integerDoubled*3);
}
}