gpt4 book ai didi

Java更改应用程序

转载 作者:行者123 更新时间:2023-11-30 08:16:50 24 4
gpt4 key购买 nike

所以我一直在研究一个程序,自从我上次使用 java 以来已经有一段时间了。我想知道如何让我的程序接受小数。我已经尝试查找它,但找不到任何有用的信息和我真正理解的任何内容。以下是我到目前为止所做的...

    package test;
import java.util.Scanner;

/**
*
* @author Thao
*/
public class Test {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
double cash; // double makes it allow decimals
int hundred;
int twenty;
int ten;
int toonies ;
int lonies;
int quarters;
int dimes;
int nickels;
int pennies;
int money;
int change;

System.out.println();// blank line
System.out.print("Hello ");
System.out.println();
System.out.println("Please enter a number with no decimal places ");
cash = input.nextInt(); // read first integer
System.out.println("you have Entered "+cash);
hundred = 100;
change = (int)hundred - (int)cash;
System.out.println("The change from $100.00 is:" + (double)change);


change = 100 * change; // multiply cash by 100
money = (int)change; //cash is now a int

twenty = money / 2000;
money = money - (twenty * 2000);

toonies = money / 200; // money is multiplied by 100 than / by 200
money = money - (toonies * 200); // ex. money = 1586 - (7.93 * 200 )

lonies = money / 100 ;
money = money - (lonies * 100);

quarters = money / 25;
money = money - (quarters * 25);

dimes = money / 10;
money = money - (dimes * 10);

nickels = money / 5;
money = money - (nickels * 5);

pennies = money / 1;
money = money - (pennies * 1);
if(twenty>0){
System.out.println();
System.out.print("You will have this many Twenties ");
System.out.print(twenty + ".");
}
else{
}
if(toonies>0){
System.out.println();
System.out.print("You will have this many Toonies ");
System.out.print(toonies + ".");
System.out.println();
}
else{
}
if(lonies>0){
System.out.print(" You will have this many Lonies ");
System.out.print(lonies + ".");
System.out.println();
}
else{
}
if(quarters>0){
System.out.print(" You will have this many quarters ");
System.out.print(quarters + ".");
System.out.println();
}
else{
}

if(dimes>0){
System.out.print(" You will have this many dimes ");
System.out.print(dimes + ".");
System.out.println();
}
else{
}

if(dimes>0){
System.out.print(" You will have this many nickels ");
System.out.print(nickels);
System.out.println();
}
else{
}
if(pennies>0){
System.out.print(" You will have this many pennies ");
System.out.print(pennies);
System.out.println();
}
else{
}
System.out.println();
System.out.println("Thank you for using my app ");


}
}

最佳答案

您可以使用 double 或 float 作为变量的数据类型。要从您的输入中读取 double,您可以这样做:

double cash = input.nextDouble();

float cash = input.nextFloat();

关于Java更改应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28117494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com