久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

字符串不能轉(zhuǎn)換為int!如何在java上對掃描的int求

string cannot be converted to int! how to sum a scanned int on java?(字符串不能轉(zhuǎn)換為int!如何在java上對掃描的int求和?)
本文介紹了字符串不能轉(zhuǎn)換為int!如何在java上對掃描的int求和?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我編寫了這個簡單的程序來求和用戶寫入的掃描 int,但是當(dāng)我編譯它,它說字符串不能轉(zhuǎn)換為int".這個程序有什么問題?

I wrote this simple program to sum a scanned int written by the user, but when i compile it, it says that "string cannot be converted to int." What is wrong in this program?

import java.util.*;
public class Pr6{
      public static void main(String[] args){
      Scanner scan = new Scanner (System.in);
      int num1;
      int num2;
      int num3;
      int sum;

                  System.out.print("Please write an integer: ");
       num1 = scan.nextLine();

                  System.out.print("Please write an integer: ");
       num2 = scan.nextLine();

                  System.out.print("Please write an integer: ");
       num3 = scan.nextLine();

       sum = num1 + num2 + num3;
                  System.out.print("Total = " + sum);



        }//main
}//Pr6

推薦答案

這是你的問題

num1 = scan.nextLine();

我們看看num1是什么數(shù)據(jù)類型:

Let's look at what data type num1 is:

int num1;

scan.nextLine() 將返回一個字符串.而且你不能有 int num1 = "1",因為它們是不同的數(shù)據(jù)類型.

scan.nextLine() will return a String. And you can't have int num1 = "1", because they are different data types.

您應(yīng)該使用 scan.nextInt().它將返回一個數(shù)字.這會解決你的問題:)

You should use scan.nextInt(). It will return a number. That'll solve your problems :)

所以你會有 num1 = scan.nextInt()num2 = scan.nextInt()num3 = scan.nextInt().

So you'll have num1 = scan.nextInt(), num2 = scan.nextInt(), and num3 = scan.nextInt().

我希望這會有所幫助.祝你好運!

I hope that helps. Good luck!

這篇關(guān)于字符串不能轉(zhuǎn)換為int!如何在java上對掃描的int求和?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 三级黄色片在线 | 欧美成人一区二免费视频软件 | 日韩视频 中文字幕 | 久久aⅴ乱码一区二区三区 亚洲欧美综合精品另类天天更新 | 日韩中文在线 | 狠狠亚洲| 中文字幕二区 | 在线观看日本高清二区 | 成人精品视频在线 | 国产黄色麻豆视频 | 国内精品久久精品 | 日本超碰 | 岛国在线免费观看 | 亚洲综合无码一区二区 | 欧美日韩一区二区在线播放 | 天天拍天天色 | 精品久久久久一区二区国产 | 国产精品99久久久久久宅男 | 国产激情小视频 | 精品国产黄a∨片高清在线 www.一级片 国产欧美日韩综合精品一区二区 | 日韩免费 | 亚洲精品精品 | 欧美日韩亚洲国产 | 国产欧美日韩一区二区三区在线观看 | 欧美一区2区三区4区公司二百 | 国产丝袜一区二区三区免费视频 | 激情福利视频 | 91久久久久 | 国产精品久久久久久久久久久久久 | 国产精品99久久久久久动医院 | 精品自拍视频 | 欧美视频在线看 | 一区二区三区欧美在线 | 亚洲免费视频在线观看 | 人成精品 | 荷兰欧美一级毛片 | 久久久久久久久久久久亚洲 | 成人午夜精品 | 成人午夜在线 | av一区二区三区四区 | 婷婷色婷婷|