鍵盤錄入代碼java,數(shù)字鍵盤錄入

java中如何實(shí)現(xiàn)用鍵盤輸入內(nèi)容到文件?

step1:新建一個(gè)演示類demo

為昭蘇等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及昭蘇網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站建設(shè)、昭蘇網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

step2:導(dǎo)入 包文件,在包名下,類名之上輸入如下代碼。

import ?java.util.Scanner;

step3:在類中的代碼如下:

public static void main(String[] args) { ? ?//創(chuàng)建一個(gè)鍵盤錄入對(duì)象input ? ?Scanner input = new Scanner(System.in); ? ?System.out.println("please input “學(xué)生姓名”"); ? ?String studentName = input.next().intern(); ? ?System.out.println("please input “科目名稱”"); ? ?String subject = input.next().intern(); ? ?System.out.println("please input“科目成績”"); ? ?double result = input.nextDouble(); ? ?//調(diào)用Student類的方法 ? ?Student stu = new Student(); ? ?stu.setStudentName(studentName); ? ?stu.setSubject(subject); ? ?stu.setResult(result); ? ?Student.getInformation(stu);}

step4:新建一個(gè)Student類,設(shè)置類的各個(gè)成員變量,創(chuàng)建一個(gè)學(xué)生個(gè)人信息的方法。如下:

public class Student { ? ?private String studentName; ? ?private String subject; ? ?private double result; ? ?private String eveluate; ? ?//創(chuàng)建一個(gè)信息輸出方法 ? ?public static void getInformation(Student studentInformation) { ? ? ? ?System.out.println("學(xué)生個(gè)人信息"); ? ? ? ?//獲取學(xué)生姓名返回的成員變量值 ? ? ? ?System.out.println("姓名:" + studentInformation.getStudentName()); ? ? ? ?//獲取科目成員變量的返回值 ? ? ? ?System.out.println("科目:" + studentInformation.getSubject()); ? ? ? ?//獲取成績成員變量的返回值 ? ? ? ?System.out.println("成績:" + studentInformation.getResult()); ? ? ? ?//獲取等級(jí)成員變量的返回值 ? ? ? ?System.out.println("等級(jí):" + studentInformation.getEveluate()); ? ?} ? ?//使用getXxx()和setXxx()對(duì)各個(gè)私有成員變量進(jìn)行限定 ? ?//對(duì)學(xué)生姓名進(jìn)行輸入和輸出的設(shè)置 ? ?public String getStudentName() { ? ? ? ?return this.studentName; ? ?} ? ?public void setStudentName(String studentName) { ? ? ? ?this.studentName = studentName; ? ?} ? ?//對(duì)成績等級(jí)變量設(shè)置 ? ?public String getEveluate() { ? ? ? ?return this.eveluate; ? ?} ? ?public void setEveluate(String eveluate) { ? ? ? ?this.eveluate = eveluate; ? ?} ? ? //對(duì)科目成員變量進(jìn)行設(shè)置 ? ?public String getSubject() { ? ? ? ?return this.subject; ? ?} ? ?public void setSubject(String subject) { ? ? ? ?this.subject = subject; ? ?} ? ?public double getResult() { ? ? ? ?return this.result; ? ?} ? ?//對(duì)成績進(jìn)行等級(jí)劃分 ? ?public void setResult(double result) { ? ? ? ?if (result = 95) { ? ? ? ? ? ?this.result = result; ? ? ? ? ? ?this.eveluate = "A+"; ? ? ? ?} else if (result 90 result = 85) { ? ? ? ? ? ?this.result = result; ? ? ? ? ? ?this.eveluate = "A"; ? ? ? ?} else if (result = 80 result 85) { ? ? ? ? ? ?this.result = result; ? ? ? ? ? ?this.eveluate = "B+"; ? ? ? ?} else if (result = 75 result 80) { ? ? ? ? ? ?this.result = result; ? ? ? ? ? ?this.eveluate = "B"; ? ? ? ?} else if (result = 70 result 75) { ? ? ? ? ? ?this.result = result; ? ? ? ? ? ?this.eveluate = "C+"; ? ? ? ?} else if (result = 60 result 70) { ? ? ? ? ? ?this.result = result; ? ? ? ? ? ?this.eveluate = "C"; ? ? ? ?} else if (result = 50 result 60) { ? ? ? ? ? ?this.result = result; ? ? ? ? ? ?this.eveluate = "D"; ? ? ? ?} else { ? ? ? ? ? ?this.result = result; ? ? ? ? ? ?this.eveluate = "E"; ? ? ? ?} ? ?}}

運(yùn)行結(jié)果1:

please input “學(xué)生姓名”

李小明

please input “科目名稱”

數(shù)學(xué)

please input“科目成績”

98

學(xué)生個(gè)人信息

姓名:李小明

科目:數(shù)學(xué)

成績:98.0

等級(jí):A+

運(yùn)行結(jié)果2:

please input “學(xué)生姓名”

王強(qiáng)

please input “科目名稱”

英語

please input“科目成績”

52

學(xué)生個(gè)人信息

姓名:王強(qiáng)

科目:英語

成績:52.0

等級(jí):D

java 中如何用鍵盤輸入

可以使用java的Scanner類,常見的是用nextInt()輸入一個(gè)整數(shù),用next()輸入一個(gè)字符串,下面是一個(gè)小的演示程序。

public?class?InputTest

{

public?static?void?main(String[]?args)

{

Scanner?input?=?new?Scanner(System.in);

System.out.println(input.nextInt());

System.out.println(input.next());

input.close();

}

}

java鍵盤輸入語句怎么寫

這樣寫就可以了

方法一

import java.util.*Scanner in=new Scanner(System.in)

System.out.println("please enter a: ")

double a=in.nextDouble

輸入aSystem.out.println("Please enter b: ")

double b=in.nextDouble

輸入bdouble c=a+b

System.out.println("The result:"+c)

輸出結(jié)果

方法二

首先定義scanner,方法:Scanner scanner = new Scanner(System.in);

此時(shí)會(huì)提示有錯(cuò)誤,需要加入頭文件:import java.util.Scanner;

(筆者用的是eclipse,按快捷鍵Ctrl+shift+O就可以了。)

從鍵盤輸入整形變量:int n=scanner.nextInt();

double類型的:double n = scanner.nextDouble();

從鍵盤輸入一個(gè)字符串:String n = scanner.next();

從鍵盤依次輸入數(shù)組中的元素:

int [] names = new int[6];

for(int i=0; i6; i++){

names[i] = scanner.nextInt();

網(wǎng)頁標(biāo)題:鍵盤錄入代碼java,數(shù)字鍵盤錄入
標(biāo)題來源:http://www.muchs.cn/article38/hssgsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、關(guān)鍵詞優(yōu)化軟件開發(fā)、企業(yè)網(wǎng)站制作、云服務(wù)器移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)