如何以不同的方式打印数字?

huangapple go评论66阅读模式
英文:

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;
    }

huangapple
  • 本文由 发表于 2020年8月8日 06:06:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63309705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定