-3

如何将我的数字四舍五入output += Math.pow(baseUno, powernumber)+ " ";到最接近的整数?

他们总是给我一个output,例如1.0 或 2.0。您如何将这些四舍五入,以便它们简单地生成1 和 2

import javax.swing.*;
import java.text.*;
import java.util.*;

public class level7Module1
{

public static void main(String[] args)
{

String base, power, output = " "; //user inputs, and the output variable
double baseUno, powerUno, basenum = 0, powernum = 0; //user inputs parsed so they can be used in mathematical equations

DecimalFormat noDigits = new DecimalFormat("0");

 // oneDigit.format(variablename)


base = JOptionPane.showInputDialog(null,"Enter your prefered base, a number between 1 and 14: \nPress 'q' to quit."); //User input
    if (base.equals("q"))
    {
    JOptionPane.showMessageDialog(null,"Goodbye!");
    System.exit(0); //quits
    }

baseUno = Integer.parseInt(base);
// basenum = noDigits.format(baseUno);

if (baseUno <= 0)
{
JOptionPane.showMessageDialog(null,"Really? Why would you try to trick me? ):");
System.exit(0);
}
if (baseUno > 14)
{
JOptionPane.showMessageDialog(null,"That wasn't cool. Take some time to think about this \nand\nthen\ntry\nagain.\n\n\n\n\n\n\n\nJerk.");
System.exit(0);
}

//I chose 0 and 14 because on my monitor the combination of 14 and 14 filled up the entire screen, so I limited to things
 //that would fit on my monitor :)

power = JOptionPane.showInputDialog(null, "How many numbers of this base would you like? Between 1 and 14 again, please.\nPress 'q' to quit.");

if (power.equals("q"))
    {
    JOptionPane.showMessageDialog(null,"Goodbye!");
    System.exit(0);
    }

powerUno = Integer.parseInt(power);
// powernum = noDigits.format(powerUno);

if (powerUno <= 0)
    {
    JOptionPane.showMessageDialog(null,"Really? Why would you try to trick me? ):");
    System.exit(0);
    }
if (powerUno > 14)
    {
    JOptionPane.showMessageDialog(null,"That wasn't cool. Take some time to think about this \nand\nthen\ntry\nagain.\n\n\n\n\n\n\n\nJerk.");
    System.exit(0);
    }

for (int powernumber=0; powernumber!=powerUno; powernumber++) //Set the number of powers done to 0, then until it's "powernum" input, it keeps going.
{
output += Math.pow(baseUno, powernumber)+ " "; //Output is the basenum to the power of powernum plus a space, then repeats condition above.
}

JOptionPane.showMessageDialog(null,"Your numbers are: " + output); //Giving the users their outputs

}
}
4

1 回答 1

0

以最简单的方法更改此行:
JOptionPane.showMessageDialog(null,"Your numbers are: " + output);
只需
JOptionPane.showMessageDialog(null,"Your numbers are: " + (int)output);
键入 caste 结果为int

于 2013-02-11T16:21:52.287 回答