久久久久久久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求和?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

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

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() 將返回一個(gè)字符串.而且你不能有 int num1 = "1",因?yàn)樗鼈兪遣煌臄?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().它將返回一個(gè)數(shù)字.這會(huì)解決你的問題:)

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

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

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

我希望這會(huì)有所幫助.祝你好運(yùn)!

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,如何獲取插入的自動(dòng)生成密鑰?[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(使用線程逐塊處理文件)
主站蜘蛛池模板: 精品小视频 | 一级毛片在线免费观看 | 黄色在线免费看 | 亚洲一区二区免费视频 | 国产一区不卡 | 成人免费视频一区二区 | 欧美精品久久久久久久多人混战 | 国产精品毛片久久久久久久 | 欧美国产日韩一区二区 | 国产三级免费观看 | 日韩精品一区二区三区免费视频 | 四虎影视最新网址 | 亚洲一级片在线观看 | 久久久黄色片 | 色综合天天综合网国产成人网 | 久久精品国产免费 | 一级黄色在线观看 | av基地网| 免费观看全黄做爰视频 | 九色91popny蝌蚪新疆 | 久久高清免费视频 | 日韩毛片网站 | www.三级| 成人av一区二区三区在线观看 | 亚洲综合一区二区三区 | 成人免费小视频 | 久久九九精品 | 91亚洲国产成人精品性色 | 91成人在线观看喷潮蘑菇 | 色婷婷久久综合 | 国产成人在线免费视频 | 日本www在线观看 | 午夜天堂av | 全部免费毛片在线播放高潮 | 久久综合伊人 | 欧美日韩免费在线 | 中文字幕在线免费观看视频 | 午夜在线免费观看 | 手机av片 | 综合久久久久 | 亚洲一级片 |