2013-02-11 6 views
-3

Как объединить мои номера output += Math.pow(baseUno, powernumber)+ " "; до конца целого числа?Округление удваивается до ближайшего целого числа в Java?

Они всегда дают мне 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 

} 
} 
+2

вы не можете просто кастовал returnrd двойной Int ?? – PermGenError

+0

[Math.round()] (http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round%28double%29)? в конечном итоге, приведя результат, который является «длинным», «int»? – ThanksForAllTheFish

+0

Я бы рекомендовал уменьшить количество кода в соответствующих частях, чтобы облегчить другим пользователям понимание вашего вопроса. – junix

ответ

0

Для простейшего изменения подхода этой линии:
JOptionPane.showMessageDialog(null,"Your numbers are: " + output);
в
JOptionPane.showMessageDialog(null,"Your numbers are: " + (int)output);
просто введите кастовой результат на int

Смежные вопросы