『壹』 java中char的控制台輸入函數是什麼
1、接收整數:
int a = 0;
try {
System.out.print("請輸入a的值:"); // 提示用戶輸入數據
a = Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine()); //將用戶輸入的值賦給a
} catch (Exception e) {
}
2、接收小數:
float f = 0.0f;
try {
System.out.print("請輸入f的值:");
f = Float.parseFloat((new BufferedReader(new InputStreamReader(System.in))).readLine());
} catch (Exception e) {
}
double e = 0.0;
try {
System.out.print("請輸入e的值:");
e = Double.parseDouble((new BufferedReader(new InputStreamReader(System.in))).readLine());
} catch (Exception e) {
}
3、接收字元的值:
char c = 0;
System.out.println("請輸入運算符:");
try {
c = (char) System.in.read();
System.in.skip(2);
} catch (Exception e) {
}
4、接收字元串的值:
String str = null;
System.out.println("請輸入字元串:");
try {
str = (new BufferedReader(new InputStreamReader(System.in))).readLine();
} catch (Exception e) {
}
『貳』 java中怎麼輸入二進制數據
Java中Scanner 是輸入函數,首先建立一個輸入函數,直接讀取輸入的二進制數據,然後通過Integer.valueOf轉換成十進制即可。
Scannerinput=newScanner(System.in);
intlength=input.nextInt();//輸入二進制數據
intlength10=Integer.valueOf(length,10)//轉換成十進制
『叄』 java新手問題——輸入函數
在Java中輸入函數需要使用到 Scanner對象。
以下是輸入函數使用的一個小例子:
public static void main(String[] args){
//構造一個Scanner對象,其傳入參數為System.in
Scanner x=new Scanner(System.in);
System.out.print("請輸入一個整數:");
//讀取一個int數值
int i=x.nextInt();
System.out.println("你剛才輸入的數為"+i);
}
結果如下:
(3)java輸入函數擴展閱讀:
在類中構造一個Scanner對象,其傳入參數為System.in
讀取鍵盤數據方法如下:
1、讀取一行文本(可帶空格),使用nextLine( )。
2、讀取一個單詞,使用next( )。
3、讀取一個int數值,使用nextInt( )。
4、讀取一個double數值 ,使用nextDouble( )。
用hasNextInt()和hasNextDouble()檢測是否還有表示int或double數值的字元序列
『肆』 java怎麼輸入數據
1
java的Scanner類提供了nextInt、nexFloat、nextDouble等方法,可以像類似C語言的scanf那樣讀取指定類型的數字。
首先定義一個Scanner對象:
Scanner sn = new Scanner(System.in);
java怎麼輸入數字
2
用sn.nextInt讀取整型數,注意如果輸入的不是整形數則該函數會拋出InputMismatchException異常,應予以捕獲。
System.out.print("請輸入一個整數:"); try{ intVal = sn.nextInt(); System.out.println("你輸入了:" + intVal); }catch(InputMismatchException e) { System.out.println("必須輸入整數!"); }
java怎麼輸入數字
3
用sn.nextFloat讀取單精度浮點數,如果輸入的不是數字則也會拋出InputMismatchException異常,應予以捕獲。
System.out.print("請輸入一個浮點數:"); try{ floatVal = sn.nextFloat(); System.out.println("你輸入了:" + floatVal); }catch(InputMismatchException e) { System.out.println("必須輸入數!"); }
java怎麼輸入數字
4
用sn.nextDouble讀取雙精度浮點數,操作與單精度類似。
System.out.print("請輸入一個浮點數:"); try{ doubleVal = sn.nextDouble(); System.out.println("你輸入了:" + doubleVal); }catch(InputMismatchException e) { System.out.println("必須輸入數!"); }
java怎麼輸入數字
5
流使用完畢後應予以關閉:sn.close();完整代碼如下圖。
java怎麼輸入數字
java怎麼輸入數字
6
測試程序,首先正常輸入,可以看到沒輸入一個數,控制台會進行回顯。
java怎麼輸入數字
7
接下來試試異常情況,在要求輸入整數時輸入小數,則會提示「必須輸入整數」,在輸入浮點數時輸入字母,則會提示「必須輸入數」,雖然操作非法,程序依然可以執行,這是捕獲了異常的緣故。
『伍』 如何用命令行運行java程序及輸入main函數參數的輸入
main函數也可以帶參數。帶參數main函數的定義格式如下:
void main(int argc, char *argv[])
{
... ...
}
argc和argv是main函數的形式參數。這兩個形式參數的類型是系統規定的。如果main函數要帶參數,就是這兩個類型的參數;否則main函數就沒有參數。變數名稱argc和argv是常規的名稱,當然也可以換成其他名稱。
那麼,實際參數是如何傳遞給main函數的argc和argv的呢?我們知道,C程序在編譯和鏈接後,都生成一個exe文件,執行該exe文件時,可以直接執行;也可以在命令行下帶參數執行,命令行執行的形式為:
可執行文件名稱 參數1 參數2 ... ... 參數n
執行文件名稱和參數、參數之間均使用空格隔開。例如,在linux下運行程序./a.out 1 2 ,可執行文件名稱為./a.out,參數1為字元串1,參數2為2。
如果按照這種方法執行,命令行字元串將作為實際參數傳遞給main函數。具體為:
(1) 可執行文件名稱和所有參數的個數之和傳遞給argc;所以上面的argc=3
(2) 可執行文件名稱(包括路徑名稱)作為一個字元串,首地址被賦給argv[0],參數1也作為一個字元串,首地址被賦給argv[1],... ...依次類推。
『陸』 用Java編寫的應用程序,輸入函數表達式生成相應的函數圖像,
java語言輸入函數表達式生成相應的函數圖像代碼如下:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class UI extends JFrame
{
MyPanel mp;
JPanel pl = new JPanel();
JPanel pl1 = new JPanel(),
pl2 = new JPanel(),
pl3 = new JPanel(),
pl4 = new JPanel();
JRadioButton rb1,rb2;
ButtonGroup bg = new ButtonGroup();
JTextField tf = new JTextField(16);
String[] s = {"y = sin(x)", "y = cos(x)", "y = tan(x)",
"y = pow(x, 2)", "y = pow(x, 3)", "y = log(x)",
"y = pow(2, x)", "y = sqrt(x)", "r = a(sita)"};
JComboBox cb;
JButton bn1 = new JButton("變寬"),
bn2 = new JButton("變窄"),
bn3 = new JButton("拉長"),
bn4 = new JButton("壓短"),
bn = new JButton("繪圖"),
exit = new JButton("退出"),
bn5 = new JButton("左移"),
bn6 = new JButton("右移"),
bn7 = new JButton("上移"),
bn8 = new JButton("下移");
public UI()
{
mp = new MyPanel(this);
pl1.setLayout(new GridLayout(1, 2));
pl2.setLayout(new GridLayout(1, 2));
pl3.setLayout(new GridLayout(1, 2));
pl4.setLayout(new GridLayout(1, 2));
pl1.add(bn1); bn1.setEnabled(false);
pl1.add(bn2); bn2.setEnabled(false);
pl2.add(bn3); bn3.setEnabled(false);
pl2.add(bn4); bn4.setEnabled(false);
pl3.add(bn5); bn5.setEnabled(false);
pl3.add(bn6); bn6.setEnabled(false);
pl4.add(bn7); bn7.setEnabled(false);
pl4.add(bn8); bn8.setEnabled(false);
pl.setLayout(new GridLayout(20, 1));
rb1 = new JRadioButton("輸入函數");
rb2 = new JRadioButton("選擇已有函數");
rb2.setSelected(true);
tf.setEnabled(false);
bg.add(rb1); bg.add(rb2);
rb1.addActionListener(mp);
rb2.addActionListener(mp);
pl.add(rb1);
pl.add(tf);
pl.add(rb2);
cb = new JComboBox(s);
pl.add(cb);
pl.add(new JLabel());
pl.add(pl1); pl.add(pl2);
pl.add(pl3); pl.add(pl4);
pl.add(bn);
pl.add(exit);
bn1.addActionListener(mp);
bn2.addActionListener(mp);
bn3.addActionListener(mp);
bn4.addActionListener(mp);
bn5.addActionListener(mp);
bn6.addActionListener(mp);
bn7.addActionListener(mp);
bn8.addActionListener(mp);
bn.addActionListener(mp);
exit.addActionListener(mp);
this.setLayout(new BorderLayout());
this.add(mp, BorderLayout.CENTER);
this.add(pl, BorderLayout.EAST);
this.setTitle("平面直角坐標系畫圖小工具");
this.setSize(797, 600 + 37);
Dimension dn = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((dn.width - 797) / 2, (dn.height - 637) / 2);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args)
{
new UI();
}
}
package math;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements ActionListener,MouseMotionListener
{
UI ui;
int flag;
double h_times;
int w_times;
int dx;
int dy;
String str;
Point pt = new Point(0, 0);
void init()
{
flag = -1;
h_times = Math.PI / 100;
w_times = 100;
dx = 300;
dy = 300;
}
public MyPanel(UI ui)
{
this.addMouseMotionListener(this);
init();
this.ui = ui;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
drawCoordinate(g2);
Line2D line;
g2.setColor(Color.BLUE);
g2.drawString("(" + (pt.x - 300) + ", " + (300 - pt.y) + ")", pt.x + 20, pt.y + 20);
switch(flag)
{
case 0:
g2.drawString("y = Asin(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sin(getReal_X(i)) * w_times, i + 1, dy - Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 1:
g2.drawString("y = Acos(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.cos(getReal_X(i)) * w_times, i + 1, dy - Math.cos(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 2:
g2.drawString("y = Atan(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.tan(getReal_X(i)) * w_times, i + 1, dy - Math.tan(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 3:
g2.drawString("y = Apow(Bx + C, 2) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 2) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 2) * w_times);
g2.draw(line);
}
break;
case 4:
g2.drawString("y = Apow(Bx + C, 3) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 3) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 3) * w_times);
g2.draw(line);
}
break;
case 5:
g2.drawString("y = Alog(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.log(getReal_X(i)) * w_times, i + 1, dy - Math.log(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 6:
g2.drawString("y = Apow(2, Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.pow(2, getReal_X(i)) * w_times, i + 1, dy - Math.pow(2, getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 7:
g2.drawString("y = Asqrt(Bx + C) + D", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(i, dy - Math.sqrt(getReal_X(i)) * w_times, i + 1, dy - Math.sqrt(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
case 8:
g2.drawString("y = a(sita)", 105, 60);
for(double i = 0; i < 600; i += 0.01)
{
line = new Line2D.Double(getReal_X(i) * Math.cos(getReal_X(i)), dy - getReal_X(i) * Math.sin(getReal_X(i)) * w_times, getReal_X(i) * Math.cos(getReal_X(i + 1)), dy - getReal_X(i) * Math.sin(getReal_X(i + 1)) * w_times);
g2.draw(line);
}
break;
}
if(flag != -1)
{
g2.drawString("A = " + w_times, 105, 90);
g2.drawString("B= " + h_times, 105, 120);
g2.drawString("C= " + (300 - dx), 105, 150);
g2.drawString("D= " + (300 - dy), 105, 180);
}
}
private double getReal_X(double x)
{
return (x - dx) * h_times;
}
private void drawCoordinate(Graphics2D g2)
{
int len = 20;
Line2D line;
for(int i = 0; i <= 600 / len; i++)
{
g2.setColor(Color.PINK.darker());
if(i == 300 / len)
g2.setColor(Color.RED);
else;
line = new Line2D.Double(0, i * len, 600, i * len);
g2.draw(line);
line = new Line2D.Double(i * len, 0, i * len, 600);
g2.draw(line);
}
drawPoint(g2, 300, 300);
}
private void drawPoint(Graphics2D g2, double x, double y)
{
g2.setColor(Color.YELLOW);
Ellipse2D circle = new Ellipse2D.Double(x - 2, y - 2, 4, 4);
g2.fill(circle);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == ui.rb1)
{
ui.tf.setEnabled(true);
ui.cb.setEnabled(false);
flag = -1;
}
if(e.getSource() == ui.rb2)
{
ui.tf.setEnabled(false);
ui.cb.setEnabled(true);
}
if(e.getSource() == ui.bn1)
{
h_times /= 1.1;
}
if(e.getSource() == ui.bn2)
{
h_times *= 1.1;
}
if(e.getSource() == ui.bn3)
{
// ui.bn4.setEnabled(true);
w_times += 10;
// if(w_times >= 300)
// ui.bn3.setEnabled(false);
}
if(e.getSource() == ui.bn4)
{
// ui.bn3.setEnabled(true);
w_times -= 10;
// if(w_times <= 0)
// ui.bn4.setEnabled(false);
}
if(e.getSource() == ui.bn5)
{
dx -= 10;
}
if(e.getSource() == ui.bn6)
{
dx += 10;
}
if(e.getSource() == ui.bn7)
{
// ui.bn8.setEnabled(true);
dy -= 10;
// if(dy <= 0)
// ui.bn7.setEnabled(false);
}
if(e.getSource() == ui.bn8)
{
// ui.bn7.setEnabled(true);
dy += 10;
// if(dy >= 600)
// ui.bn8.setEnabled(false);
}
if(e.getSource() == ui.bn)
{
if(ui.tf.isEnabled() == true)
{
str = ui.tf.getText();
if(str == null || str.length() == 0)
{
ui.bn1.setEnabled(false);
ui.bn2.setEnabled(false);
ui.bn3.setEnabled(false);
ui.bn4.setEnabled(false);
ui.bn5.setEnabled(false);
ui.bn6.setEnabled(false);
ui.bn7.setEnabled(false);
ui.bn8.setEnabled(false);
JOptionPane.showMessageDialog(this, "請輸入函數表達式 !");
return;
}
}else flag = -2;
ui.bn1.setEnabled(true);
ui.bn2.setEnabled(true);
ui.bn3.setEnabled(true);
ui.bn4.setEnabled(true);
ui.bn5.setEnabled(true);
ui.bn6.setEnabled(true);
ui.bn7.setEnabled(true);
ui.bn8.setEnabled(true);
init();
if(ui.cb.isEnabled() == true)
{
flag = ui.cb.getSelectedIndex();
}
}
if(e.getSource() == ui.exit)
System.exit(0);
repaint();
}
public void mouseDragged(MouseEvent arg0)
{
}
public void mouseMoved(MouseEvent e)
{
pt = e.getPoint();
repaint();
}
}
『柒』 JAVA的輸入函數,Scanner in=new Scanner(System.in);請教一下!
Scanner是util5.0包自帶的一個類,它封裝了InputStreamReader
System.in其實就是獲得一個鍵盤輸入流,監聽鍵盤輸入的數據
所以這句話Scanner in=new Scanner(System.in);
就已經得到了獲取鍵盤輸入的實例,但是in 和System.in不是一個東西,
Scanner提供了很多中方法,不光有nextDouble(); 你可以查看API有非常多的方法,明白了么?
『捌』 java 鍵盤輸入函數
importjava.io.*;
importjava.util.Scanner;//導入Scanner對象;
publicclassCompareTwoNumbers{
publicstaticvoidmain(Stringargs[]){
Doublenumber1,number2;
System.out.print("請輸入第一個數:");
Scannerscanner=newScanner(System.in);//實例化一個Scanner對象;
number1=scanner.nextDouble();//從鍵盤輸入一個實數賦值給number1;
System.out.print("請輸入第二個數:");
number2=scanner.nextDouble();//從鍵盤輸入一個實數賦值給number1;
if(number1<number2)
System.out.println("較大的數:"+number2);
elseif(number1>number2)System.out.println("較大的數:"+number1);
elseSystem.out.println("兩個數相同");
}
}
『玖』 java輸入語句scanner
java對於一些小夥伴們來說真的是覺得難於上青天,還沒有開始學習就各種抱怨了,還擔心自己學不會,其實不是這樣的,大家把心態放平,遇到問題一步一步的來解決,你就會覺得java還是一個神奇的操作。今天我來跟大家分享關於java輸入語句scanner的操作步驟,相信學習了一下的操作步驟,關於scanner語句你就可以輕松的操作出來了,具體的步驟操作就在下方,小夥伴們快來認真的看一看吧!相信會幫到你~
1.首先,大家可以看到我在java上邊輸入的scanner語句,(如下圖紅色圈出部分所示)。
2.我們可以看到最後的運行結果,(如下圖所示)。
3.最後,給小夥伴們解釋一下部分說明:importjava.util.Scanner的作用是來構建Scanner對象。System.in作為參數傳遞給Scanner的構造函數,使Scanner用鍵盤作為輸入,然後用new在內存中實例化一個Scanner出來,其它變數就可以調用這塊內存區域。
以上的操作步驟就是java輸入語句scanner的具體操作步驟,已經詳細的給小夥伴們羅列出來了,大家按照以上的操作步驟按步就班的進行操作就可以正確輸入語句scanner,是不是看了以上步驟也不覺得很困難了。最後,希望以上的操作步驟對你有所幫助!
本篇文章使用以下硬體型號:聯想小新Air15;系統版本:win10;軟體版本:JAVA2017。
『拾』 java的main函數里string可以有輸入嗎
可以。寫java程序時main函數必須有一個字元串數組即String[]args。作用:用來獲取用戶從命令行輸入的參數,如果main函數中不寫字元串數組,則將會報錯。