Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从double和输入tsking中使用printf给出两个小数_Java - Fatal编程技术网

Java 从double和输入tsking中使用printf给出两个小数

Java 从double和输入tsking中使用printf给出两个小数,java,Java,该代码旨在计算ansa表达式,并生成一个小数点后两位的答案,其中viriable声明为double。 谢谢你的帮助 nextInt函数读取整数。您需要使用nextDouble函数来读取double 此外,您需要在输入双精度后进行计算。printf('答案是%0.2f',ans)。您的代码不正确。因为他给出的输入示例为1.75 Expected Input: x=1.75 y=1.25 z=0.75 The answer is 0.95 This program output: Enter th

该代码旨在计算ansa表达式,并生成一个小数点后两位的答案,其中viriable声明为double。 谢谢你的帮助


nextInt函数读取整数。您需要使用nextDouble函数来读取double


此外,您需要在输入双精度后进行计算。

printf('答案是%0.2f',ans)
。您的代码不正确。因为他给出的输入示例为1.75
Expected Input:
x=1.75
y=1.25
z=0.75
The answer is 0.95

This program output:
Enter the value of x:
1.75
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at evaluate.main(evaluate.java:13)




import java.util.Scanner;   //make scanner class available
public class evaluate
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
double x=0.0;
double y=0.0;
double z=0.0;
double ans;
ans=(x*y-y*z)/x*z ;
System.out.println("Enter the value of x:"); //take input 
x=input.nextInt();
System.out.println("Enter the value of y:");  
y=input.nextInt();
System.out.println("Enter the value of z:");  //take value of z
z=input.nextInt();
System.out.printf("The answer is ","%0.2f%ans",ans); //get to two decimals

}
}