Java 字符串输出以向用户显示消息

Java 字符串输出以向用户显示消息,java,string,user-interface,boolean,biginteger,Java,String,User Interface,Boolean,Biginteger,当使用我的代码时,我收到一条输出消息,说明我输入的每个数字都是素数,即使它不是素数。如何更改输出消息以反映正确的是或不是主要结果?这是我的密码: public static void main (String [] args){ //prompt user to input a number String input = JOptionPane.showInputDialog("Enter number "); // change string to int int number =

当使用我的代码时,我收到一条输出消息,说明我输入的每个数字都是素数,即使它不是素数。如何更改输出消息以反映正确的是或不是主要结果?这是我的密码:

public static void main (String [] args){
//prompt user to input a number

String input = JOptionPane.showInputDialog("Enter number "); 
// change string to int
    int number = Integer.parseInt(input); 

//display message to user of their results
    BigInteger num = new BigInteger(input); 

    String output = number + " is" + (BigInteger(input) ? " " : " not ") + "a prime     number: " + BigInteger(input);

        JOptionPane.showMessageDialog (null, output);
}

public静态布尔值IsPrime(biginger num){
//检查数字是否为2的倍数
if(num.mod(新的BigInteger(“2”)).compareTo(BigInteger.ZERO)==0){
返回false;
}//如果没有,那就检查一下赔率

对于(BigInteger i=new BigInteger(“3”);i.multiply(i).compareTo(num)我认为您在这里遇到了一个问题-

String output = number + " is" 
    + (BigInteger(input) ? " " : " not ") + "a prime     number: " 
    + BigInteger(input);
你想要更像这样的东西吗-

String output = num + " is" 
    + (IsPrime(num) ? " " : " not ") + "a prime number.";
我测试了您的
IsPrime
函数,它正确地将5标识为素数,将4标识为非素数。您可能应该将其重命名为
IsPrime
,以符合Java命名约定

编辑

public static void main(String[] args) {
    // prompt user to input a number

    String input = JOptionPane.showInputDialog("Enter number ");
    // change string to int
    int number = Integer.parseInt(input);

    // display message to user of their results
    BigInteger num = new BigInteger(input);

    String output = num + " is" + (IsPrime(num) ? " " : " not ")
            + "a prime number.";

    JOptionPane.showMessageDialog(null, output);
}


我想你的主代码是错的;试试这个

public static void main (String [] args){
        //prompt user to input a number

        String input = JOptionPane.showInputDialog("Enter number "); 
        // change string to int
        int number = Integer.parseInt(input); 

        //display message to user of their results
        BigInteger num = new BigInteger(input); 

        String output = number + " is" + (IsPrime(num) ? " " : " not ") + "a prime number: " + number;

        JOptionPane.showMessageDialog (null, output);
    }

Angelo

@elliotfrisch我更改了代码,以反映您给我的内容,输入数字后,程序将终止,而不提供我的is或is primeresult@user3520570我给了你一行代码。你到底更改了什么?因为更改一行代码使你的程序在这里工作。@elliotfrisch我刚刚更改了它一行:publicstaticvoidmain(String[]args){//提示用户输入一个数字String input=JOptionPane.showInputDialog(“输入数字”);//将字符串更改为int number=Integer.parseInt(input);//向用户显示其结果的消息biginger num=new biginger(input);String output=num+“是”+(IsPrime(num)?:“not”)+“一个素数。”JOptionPane.showMessageDialog(null,output);@user3520570我编辑了包含main的全部代码(我没有更改
IsPrime
);我添加了消息的屏幕截图我得到…一个用于输入,一个用于4,一个用于5。我刚刚意识到我的布尔值下有不必要的代码,这妨碍了它正常运行…非常感谢您的帮助!!
public static void main (String [] args){
        //prompt user to input a number

        String input = JOptionPane.showInputDialog("Enter number "); 
        // change string to int
        int number = Integer.parseInt(input); 

        //display message to user of their results
        BigInteger num = new BigInteger(input); 

        String output = number + " is" + (IsPrime(num) ? " " : " not ") + "a prime number: " + number;

        JOptionPane.showMessageDialog (null, output);
    }