我无法获得" system.out.print"在方法displayNumberPlus10,displayNumberPlus100,displayNumberPlus1000中工作。有什么我做错了吗?
public class ArithmeticMethods {
public static void main(String[] args){
int cats = 46;
int dogs = 25;
}
public static void displayNumberPlus10(){
int cats = 46;
int dogs = 25;
System.out.print("There were" + cats + "now we will add 10 more."
+ (cats + 10) + ".");
System.out.print("There were" + dogs + "now we will add 10 more."
+ (dogs + 10) + ".");
}
public static void displayNumberPlus100(){
int cats = 46;
int dogs = 25;
System.out.print("There were" + cats + "now we will add 100 more "
+ (cats + 100) + ".");
System.out.print("There were" + dogs + "now we will add 100 more "
+ (dogs + 100) + ".");
}
public static void displayNumberPlus1000(){
int cats = 46;
int dogs = 25;
System.out.print("There were" + cats + "now we will add 1000 more "
+ (cats + 1000) + ".");
System.out.print("There were" + dogs + "now we will add 1000 more."
+ (dogs + 1000) + ".");
}
}
答案 0 :(得分:2)
您必须在main方法中调用这些方法。在main方法中添加以下代码。
displayNumberPlus10();
displayNumberPlus100();
displayNumberPlus1000();
此外,为了更好地增强您的代码,而不是在每个方法中编写此代码:
int cats = 46;
int dogs = 25;
你可以用你所有的方法传递猫和狗,当你用你的主方法打电话时,也可以传入猫和狗。所以它看起来像这样:
public class ArithmeticMethods {
public static void main(String[] args){
int cats = 46;
int dogs = 25;
displayNumberPlus10(cats, dogs);
displayNumberPlus100(cats, dogs);
displayNumberPlus1000(cats, dogs);
}
public static void displayNumberPlus10(int cats, int dogs){
System.out.println("There were" + cats + "now we will add 10 more."
+ (cats + 10) + ".");
System.out.println("There were" + dogs + "now we will add 10 more."
+ (dogs + 10) + ".");
}
public static void displayNumberPlus100(int cats, int dogs){
System.out.println("There were" + cats + "now we will add 100 more "
+ (cats + 100) + ".");
System.out.println("There were" + dogs + "now we will add 100 more "
+ (dogs + 100) + ".");
}
public static void displayNumberPlus1000(int cats, int dogs){
System.out.println("There were" + cats + "now we will add 1000 more "
+ (cats + 1000) + ".");
System.out.println("There were" + dogs + "now we will add 1000 more."
+ (dogs + 1000) + ".");
} }
希望有所帮助。
答案 1 :(得分:0)
您没有调用任何方法。你需要在main方法中调用方法来执行它们,然后它将打印
public class ArithmeticMethods {
public static void main(String[] args){
int cats = 46;
int dogs = 25;
//Call your methods as given below
displayNumberPlus10();
displayNumberPlus100();
displayNumberPlus1000();
}
public static void displayNumberPlus10(){
int cats = 46;
int dogs = 25;
System.out.print("There were" + cats + "now we will add 10 more."
+ (cats + 10) + ".");
System.out.print("There were" + dogs + "now we will add 10 more."
+ (dogs + 10) + ".");
}
public static void displayNumberPlus100(){
int cats = 46;
int dogs = 25;
System.out.print("There were" + cats + "now we will add 100 more "
+ (cats + 100) + ".");
System.out.print("There were" + dogs + "now we will add 100 more "
+ (dogs + 100) + ".");
}
public static void displayNumberPlus1000(){
int cats = 46;
int dogs = 25;
System.out.print("There were" + cats + "now we will add 1000 more "
+ (cats + 1000) + ".");
System.out.print("There were" + dogs + "now we will add 1000 more."
+ (dogs + 1000) + ".");
}
}
答案 2 :(得分:0)
你可以试试这个
class ArithmeticMethods {
public static void main(String[] args){
int cats = 46;
int dogs = 25;
displayNumberPlus10(cats,dogs);
displayNumberPlus100(cats,dogs);
displayNumberPlus1000(cats,dogs);
}
public static void displayNumberPlus10(int c,int d){
int cats = c;
int dogs = d;
System.out.print("There were" + cats + "now we will add 10 more."
+ (cats + 10) + ".");
System.out.print("There were" + dogs + "now we will add 10 more."
+ (dogs + 10) + ".");
}
public static void displayNumberPlus100(int c,int d){
int cats = c;
int dogs = d;
System.out.print("There were" + cats + "now we will add 100 more "
+ (cats + 100) + ".");
System.out.print("There were" + dogs + "now we will add 100 more "
+ (dogs + 100) + ".");
}
public static void displayNumberPlus1000(int c,int d){
int cats = c;
int dogs = d;
System.out.print("There were" + cats + "now we will add 1000 more "
+ (cats + 1000) + ".");
System.out.print("There were" + dogs + "now we will add 1000 more."
+ (dogs + 1000) + ".");
}
}