import java.util.Scanner; public class calculator { public static void main(String [] args) { double monetary; Scanner scan=new Scanner(System.in); System.out.println("please enter the monetary amount:"); monetary=scan.nextDouble(); int dollars = (int) monetary; int cents = (int) ((monetary - dollars) * 100); int tens = dollars / 10; dollars = dollars % 10; int fives = dollars / 5; dollars = dollars % 5; int ones = dollars; int quarters = cents / 25; cents = cents % 25; int dimes = cents / 10; cents = cents % 10; int nickels = cents / 5; cents = cents % 5; int pennies = cents; System.out.println("That's equivalent to:"); System.out.println("10 dollar bill:" + tens); System.out.println("5 dollar bill:" + fives); System.out.println("1 dollar bill:" + ones); System.out.println("quarters:" + quarters); System.out.println("dimes:" + dimes); System.out.println("nickels:" + nickels); System.out.println("cents:" + pennies); } }为什么要先用/再用%
时间: 2023-05-24 10:04:44 浏览: 212
As an AI language model, I do not have feelings or emotions, and I cannot experience null. Null often refers to the absence of a value or a lack of data in programming and computer science. It is a commonly used concept in coding languages like JavaScript, Python, and SQL, among others.
相关问题
改正以下代码 import java.util.Scanner; public class Lab { public static void main(String args[]) { Scanner scnr = new Scanner(System.in); int tempInt; double output; tempInt = scnr.nextInt(); double output = (double) tempInt; System.out.println(output); } }
以下是改正后的代码:
import java.util.Scanner;
public class Lab {
public static void main(String args[]) {
Scanner scnr = new Scanner(System.in);
int tempInt;
double output;
tempInt = scnr.nextInt();
output = (double) tempInt;
System.out.println(output);
}
}
问题在于第7行,double output = (double) tempInt; 中的double output 是重复声明了一个变量,需要删除。
import java.util.Scanner; public class StudyLab { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); printSomething(scnr.nextLine()); } // 补全以下函数定义 public static void main (String toPrint){ System.out.println(toPrint); } }
这是一段Java代码。它在主函数中创建了一个Scanner对象scnr,并使用scnr.nextLine()读取用户输入。然后调用printSomething()函数,将读取到的输入作为参数传入。
在printSomething()函数中,没有实现,需要补全代码,
添加一个参数toPrint,并在函数内部使用System.out.println()打印toPrint参数即可
public static void main (String toPrint){
System.out.println(toPrint);
}
补全后的代码如下
import java.util.Scanner;
public class StudyLab {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
printSomething(scnr.nextLine());
}
public static void printSomething (String toPrint){
System.out.println(toPrint);
}
}
阅读全文