英文:
How do I print numbers in a different method?
问题
output += String.format("确保数字被打印:\n=> 输出 += String.format("数字 %d 和 %d 是Primetwin", number1, number2);\nnumber1和number2的内容在giveResultPrimeTwin方法中是未知的\n\n建议?", number1, number2);
package ui;
import domein.MyNumbers;
import java.util.Scanner;
public class PrimeTwinApplication {
public static void main(String args[]) {
int number1, number2;
PrimeTwinApplication numbers = new PrimeTwinApplication();
number1 = numbers.inputNumber("给一个数字:");
number2 = numbers.inputNumber("给一个数字:");
MyNumbers mg = new MyNumbers(number1, number2);
System.out.printf(numbers.giveResultPrimeTwin(mg));
}
private int inputNumber(String question) {
Scanner input = new Scanner(System.in);
int number;
System.out.print(question);
number = input.nextInt();
return number;
}
private String giveResultPrimeTwin(MyNumbers mg) {
String output = "";
if (mg.isPrimeTwin() == true)
output += String.format("数字 %d 和 %d 是Primetwin", mg.getNumber1(), mg.getNumber2());
else
output += String.format("数字 %d 和 %d 不是Primetwin", mg.getNumber1(), mg.getNumber2());
return output;
}
}
英文:
How do I make sure the numbers are printed?
=> output += String.format("The numbers %d and %d are a Primetwin", number1, number2);
the contents of number1 and number2 are not known within the method giveResultPrimeTwin
Suggestions?
package ui;
import domein.MyNumbers;
import java.util.Scanner;
public class PrimeTwinApplication {
public static void main (String args[] )
{
int number1, number2;
PrimeTwinApplication numbers = new PrimeTwinApplication ();
number1 = numbers.inputNumber("Give a number:");
number1 = numbers.inputNumber("Give a number:");
MyNumbers mg = new MyNumbers (number1, number2);
System.out.printf(numbers.giveResultPrimeTwin(mg));
}
private int inputNumber (String question)
{
Scanner input = new Scanner (System.in);
int number;
System.out.print(question);
number= input.nextInt();
return number;
}
private String giveResultPrimeTwin(MyNumbers mg)
{
String output = "";
if (mg.isPrimeTwin() == true)
output += String.format("The numbers %d and %d are a Primetwin", number1, number2);
else
output += String.format("The numbers %d and %d are not a Primetwin", number1, number2);
return output;
}
答案1
得分: 1
你需要将number1和number2设置为PrimeTwinApplication类的实例属性。然后你可以在PrimeTwinApplication函数中的任何地方使用它们。
package ui;
import domein.MyNumbers;
import java.util.Scanner;
public class PrimeTwinApplication {
int number1;
int number2;
public static void main(String args[])
{
PrimeTwinApplication numbers = new PrimeTwinApplication();
numbers.number1 = numbers.inputNumber("请输入一个数字:");
numbers.number2 = numbers.inputNumber("请输入一个数字:");
MyNumbers mg = new MyNumbers(numbers.number1, numbers.number2);
System.out.printf(numbers.giveResultPrimeTwin(mg));
}
private int inputNumber(String question)
{
Scanner input = new Scanner(System.in);
int number;
System.out.print(question);
number= input.nextInt();
return number;
}
private String giveResultPrimeTwin(MyNumbers mg)
{
String output = "";
if (mg.isPrimeTwin() == true)
output += String.format("数字 %d 和 %d 是孪生质数", number1, number2);
else
output += String.format("数字 %d 和 %d 不是孪生质数", number1, number2);
return output;
}
}
英文:
You need to make number1 and number 2 instance properties of PrimeTwinApplication class. Then you can use it anywhere in PrimeTwinApplication functions.
package ui;
import domein.MyNumbers;
import java.util.Scanner;
public class PrimeTwinApplication {
int number1;
int number2;
public static void main(String args[])
{
PrimeTwinApplication numbers = new PrimeTwinApplication();
numbers.number1 = numbers.inputNumber("Give a number:");
numbers.number2 = numbers.inputNumber("Give a number:");
MyNumbers mg = new MyNumbers(numbers.number1, numbers.number2);
System.out.printf(numbers.giveResultPrimeTwin(mg));
}
private int inputNumber(String question)
{
Scanner input = new Scanner(System.in);
int number;
System.out.print(question);
number= input.nextInt();
return number;
}
private String giveResultPrimeTwin(MyNumbers mg)
{
String output = "";
if (mg.isPrimeTwin() == true)
output += String.format("The numbers %d and %d are a Primetwin", number1, number2);
else
output += String.format("The numbers %d and %d are not a Primetwin", number1, number2);
return output;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论