日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

课上动手动脑

發(fā)布時(shí)間:2025/7/25 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 课上动手动脑 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Addition.java

兩數(shù)相加

源代碼:

// An addition program

?

import javax.swing.JOptionPane;? // import class JOptionPane

?

public class Addition {

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

?? {

????? String firstNumber,?? // first string entered by user

???????????? secondNumber;? // second string entered by user

????? int number1,????????? // first number to add

????????? number2,????????? // second number to add

????????? sum;????????????? // sum of number1 and number2

?

????? // read in first number from user as a string

????? firstNumber =

???????? JOptionPane.showInputDialog( "Enter first integer" );

?

????? // read in second number from user as a string

????? secondNumber =

???????? JOptionPane.showInputDialog( "Enter second integer" );

?

????? // convert numbers from type String to type int

????? number1 = Integer.parseInt( firstNumber );

????? number2 = Integer.parseInt( secondNumber );

?

????? // add the numbers

????? sum = number1 + number2;

?

????? // display the results

????? JOptionPane.showMessageDialog(

???????? null, "The sum is " + sum, "Results",

???????? JOptionPane.PLAIN_MESSAGE );

?

????? System.exit( 0 );?? // terminate the program

?? }

}

感悟:通過(guò)彈出對(duì)話框的形式先后輸入兩個(gè)數(shù)并最后計(jì)算兩數(shù)之和。。

Text Double.Java

源代碼:

package 雙精度;

?

public class TestDouble {

public static void main(String args[]) {

??? System.out.println("0.05+0.01="+(0.05+0.01));

??? System.out.println("1.0-0.42="+(1.0-0.42));

??? System.out.println("4.015*100="+(4.015*100));

??? System.out.println("123.3/100"+(123.3/100));

}

}

感悟:雙精度直接使用會(huì)產(chǎn)生誤差;

EnumTest.java

源代碼:

?

public class EnumTest {

?

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

??????? Size s=Size.SMALL;

??????? Size t=Size.LARGE;

??????? //s和t引用同一個(gè)對(duì)象?

??????? System.out.println(s==t);? //

??????? //是原始數(shù)據(jù)類型嗎?

??????? System.out.println(s.getClass().isPrimitive());

??????? //從字符串中轉(zhuǎn)換

??????? Size u=Size.valueOf("SMALL");

??????? System.out.println(s==u);? //true

??????? //列出它的所有值

??????? for(Size value:Size.values()){

??????????? System.out.println(value);

??????? }

??? }

?

}

?enum Size{SMALL,MEDIUM,LARGE};

感悟:


InputTest.java

/**

?? @version 1.10 2004-02-10

?? @author Cay Horstmann

*/

?

import java.util.*;

?

public class InputTest

{?

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

?? {?

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

?

????? // get first input

????? System.out.print("What is your name? ");

????? String name = in.nextLine();

?

????? // get second input

????? System.out.print("How old are you? ");

????? int age = in.nextInt();

?????

?????

??? /* int i;

???? String value="100";

???? i=Integer.parseInt(value);

???? i=200;

???? String s=String.valueOf(i);*/

????

????? // display output on console

????? System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));

?

?????

?? }

}

感悟:

通過(guò)對(duì)話框的彈出先輸入姓名年齡最后輸出明年此人的年齡;

?

RandomTest.java

源代碼:

?

?

?

public class RandomStr

{

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

?????? {

????????????? //定義一個(gè)空字符串

????????????? String result = "";

????????????? //進(jìn)行6次循環(huán)

????????????? for(int i = 0 ; i < 6 ; i ++)

????????????? {

???????????????????? //生成一個(gè)97~122的int型的整數(shù)

???????????????????? int intVal = (int)(Math.random() * 26 + 97);

???????????????????? //將intValue強(qiáng)制轉(zhuǎn)換為char后連接到result后面

???????????????????? result = result + (char)intVal;

????????????? }

????????????? //輸出隨機(jī)字符串

????????????? System.out.println(result);

????? }

}

感悟:通過(guò)循環(huán)產(chǎn)生隨機(jī)數(shù),強(qiáng)制轉(zhuǎn)化類型產(chǎn)生隨機(jī)的六位小寫(xiě)字母;

?


SwitchTest.java

源代碼:

?

// Drawing shapes

import java.awt.Graphics;

import javax.swing.*;

?

public class SwitchTest extends JApplet {

?? int choice;??

?

?? public void init()

?? {

????? String input;

?

????? input = JOptionPane.showInputDialog(

???????????????? "Enter 1 to draw lines\n" +

???????????????? "Enter 2 to draw rectangles\n" +

???????????????? "Enter 3 to draw ovals\n" );

?

????? choice = Integer.parseInt( input );

?? }

?

?? public void paint( Graphics g )

?? {

????? for ( int i = 0; i < 10; i++ ) {

???????? switch( choice ) {

??????????? case 1:

?????????????? g.drawLine( 10, 10, 250, 10 + i * 10 );

?????????????? break;

??????????? case 2:

?????????????? g.drawRect( 10 + i * 10, 10 + i * 10,

?????????????????????????? 50 + i * 10, 50 + i * 10 );

?????????????? break;

??? ????????case 3:

?????????????? g.drawOval( 10 + i * 10, 10 + i * 10,

?????????????????????????? 50 + i * 10, 50 + i * 10 );

?????????????? break;

??????????? default:

?????????????? JOptionPane.showMessageDialog(

????????????????? null, "Invalid value entered" );

???????? } // end switch

????? } // end for

?? } // end paint()

} // end class SwitchTest

?

/**************************************************************************

?* (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall.???? *

?* All Rights Reserved.?????????????????????????????????????????????????? *

?*??????????????????????????????????????????????????????????????????????? *

?* DISCLAIMER: The authors and publisher of this book have used their???? *

?* best efforts in preparing the book. These efforts include the????????? *

?* development, research, and testing of the theories and programs??????? *

?* to determine their effectiveness. The authors and publisher make?????? *

?* no warranty of any kind, expressed or implied, with regard to these??? *

?* programs or to the documentation contained in these books. The authors *

?* and publisher shall not be liable in any event for incidental or?????? *

?* consequential damages in connection with, or arising out of, the?????? *

?* furnishing, performance, or use of these programs.???????????????????? *

?*************************************************************************/

運(yùn)行截圖:

Test.java

源代碼:

?

public class Test {

public static void main(String[] args) {

?????? int intValue=100;

?????? long longValue=intValue;

?????? double doubleValue=1234567890;

?????? float floatValue=(float)doubleValue;

?????? System.out.println(floatValue);//1.23456794E9

??????

?????? int X=100;

?????? int Y=200;

?????? System.out.println("X+Y="+X+Y);

?????? System.out.println(X+Y+"=X+Y");

?????? doNotRunme();

??????

?????? String string="";

?????? double d1=1000.123;

?????? double d2=1000.123;

?????? if(Math.abs(d2-d1)<1e-10){

?????????????

?????? }

?????? //System.out.println(string);

?

}

?

public static void doNotRunme()

{

?????? doNotRunme();

}

}

運(yùn)行截圖:

?

TestBigDecimal.java

源代碼:

?

import java.math.BigDecimal;

?

public class TestBigDecimal

{

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

?????? {

????????????? BigDecimal f1 = new BigDecimal("0.05");

????????????? BigDecimal f2 = BigDecimal.valueOf(0.01);

????????????? BigDecimal f3 = new BigDecimal(0.05);

????????????? System.out.println("下面使用String作為BigDecimal構(gòu)造器參數(shù)的計(jì)算結(jié)果:");

????????????? System.out.println("0.05 + 0.01 = " + f1.add(f2));

????????????? System.out.println("0.05 - 0.01 = " + f1.subtract(f2));

????????????? System.out.println("0.05 * 0.01 = " + f1.multiply(f2));

????????????? System.out.println("0.05 / 0.01 = " + f1.divide(f2));

????????????? System.out.println("下面使用double作為BigDecimal構(gòu)造器參數(shù)的計(jì)算結(jié)果:");

????????????? System.out.println("0.05 + 0.01 = " + f3.add(f2));

????????????? System.out.println("0.05 - 0.01 = " + f3.subtract(f2));

????????????? System.out.println("0.05 * 0.01 = " + f3.multiply(f2));

????????????? System.out.println("0.05 / 0.01 = " + f3.divide(f2));

?????? }

}

運(yùn)行結(jié)果:對(duì)比兩種方法得出誤差大小

?

轉(zhuǎn)載于:https://www.cnblogs.com/jinseliunian/p/9752396.html

總結(jié)

以上是生活随笔為你收集整理的课上动手动脑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。