久久久久久久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网站 | 羞羞免费网站 | 精品欧美乱码久久久久久1区2区 | 麻豆一区一区三区四区 | 精品国产一区二区三区久久狼黑人 | 国产视频久久久 | 日韩欧美在线不卡 | 国产一区二区日韩 | 亚洲国产18 | 成人免费一区二区三区视频网站 | 天天成人综合网 | 国产成在线观看免费视频 | 99久热 | 国产一区二区三区四区 | 久久久无码精品亚洲日韩按摩 | 国产视频一区在线 | 涩色视频在线观看 | 亚洲一区 | a在线观看| 久久99视频 | 男人的天堂avav | 亚洲欧美在线观看 | 野狼在线社区2017入口 | 特黄色毛片 | 91中文字幕在线观看 | 天天插天天干 | 在线中文字幕视频 | 久久毛片| 人人射人人草 | 久久久国产一区二区三区四区小说 | 精品粉嫩超白一线天av | 黄色在线免费观看 | 中文字字幕一区二区三区四区五区 | 欧美精品一区二区三区一线天视频 | 成年女人免费v片 | 日韩第1页| 一级毛片在线播放 | www.亚洲 | 国产欧美在线一区 |