gpt4 book ai didi

Java 扫描器 - 双重错误

转载 作者:行者123 更新时间:2023-12-02 04:29:49 27 4
gpt4 key购买 nike

我的扫描仪有问题。如果我尝试使用 double,控制台会给出错误消息:

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.nextDouble(Scanner.java:2413) at test.test.main(test.java:9)

代码

package test;

import java.util.Scanner;

public class test {
public static void main(String args[]){
System.out.print("vlozte hmotnost: ");
Scanner vstup = new Scanner (System.in);
double hmotnost = vstup.nextDouble();

//------------------------------------------

System.out.print("vlozte drahu: ");
Scanner vstup2 = new Scanner(System.in);
double draha = vstup2.nextDouble();

//------------------------------------------

double sila = hmotnost * 10;

//------------------------------------------

double praca = sila * draha;
System.out.print("praca je: ");
System.out.print(praca);
System.out.println(" Joulov");

最佳答案

输入 1,5 而不是 1.5。另外,如上所述,您可以使用一台扫描仪:

import java.util.Scanner;

public class test {
public static void main(String args[]) {
System.out.print("vlozte hmotnost: ");
Scanner vstup = new Scanner(System.in);
double hmotnost = vstup.nextDouble();

//------------------------------------------

System.out.print("vlozte drahu: ");
double draha = vstup.nextDouble();

//------------------------------------------

double sila = hmotnost * 10;

//------------------------------------------

double praca = sila * draha;
System.out.print("praca je: ");
System.out.print(praca);
System.out.println(" Joulov");
}
}

关于Java 扫描器 - 双重错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31613616/

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