java第二节课 java语法基础动手动脑
动手动脑1:枚举变量 运行EnumTest.java
Size s=Size.SMALL;
Size t=Size.LARGE;
//s和t引用同一个对象?
System.out.println(s==t); //
//是原始数据类型吗?
System.out.println(s.getClass().isPrimitive());
//从字符串中转换
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};
枚举类型是Java 5中新增特性的一部分,它是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁性、安全性以及便捷性。这个题目中这个枚举变量中定义 Small middle large三种。Size.SMALL!=Size.MIddle后面是枚举变量的foreach迭代。枚举变量不属于原始数据类型,他的每个具体值都引用一个特定的对象。相同的值引用同一个对象。
动手动脑2:数值的二进制表示。
原码表示法是机器数的一种简单的表示法。其符号位用0表示正号,用:表示负号,数值一般用二进制形式表示。设有一数为x,则原码表示可记作〔x〕原。
机器数的补码可由原码得到。如果机器数是正数,则该机器数的补码与原码一样;如果机器数是负数,则该机器数的补码是对它的原码(除符号位外)各位取反,并在未位加1而得到的。设有一数X,则X的补码表示记作〔X〕补。
机器数的反码可由原码得到。如果机器数是正数,则该机器数的反码与原码一样;如果机器数是负数,则该机器数的反码是对它的原码(符号位除外)各位取反而得到的。设有一数X,则X的反码表示记作〔X〕反。
public static void main(String[]args)
{
int a=15;
int b=2;
System.out.println(a&b);
System.out.println(a|b);
System.out.println(a^b);
}
示例1:两数相加
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
firstNumber =
JOptionPane.showInputDialog( "Enter first integer" );
secondNumber =
JOptionPane.showInputDialog( "Enter second integer" );
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
sum = number1 + number2;
JOptionPane.showMessageDialog(
null, "The sum is " + sum, "Results",
JOptionPane.PLAIN_MESSAGE );
}
}
动手实验1:运行TestDouble.java
public class TestDouble {
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));
}
}
使用double类型的数值进行计算是,其结果是不精确的。
解决方案1:使用BigDecimal类 BigDecimal.java
注意,在构建BigDecima对象时应该使用字符串而不是double数值,否则,仍有可能引发计算精度问题。字符串转为数字的基本方法 int number=Integer。parseInt(numberString);
动手动脑:以下代码的输出结果是啥?
public static void main(String[]args)
{
int X=100;
int Y=200;
System.out.println("X+Y="+X+Y);
System.out.println(X+Y+"=X+Y");
}
测试:InputTest.java
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("What is your name? ");
String name = in.nextLine();
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));
}
}
测试:RandomStr
{
public static void main(String[] args)
{
//定义一个空字符串
String result = "";
//进行6次循环
for(int i = 0 ; i < 6 ; i ++)
{
//生成一个97~122的int型的整数
int intVal = (int)(Math.random() * 26 + 97);
//将intValue强制转换为char后连接到result后面
result = result + (char)intVal;
}
//输出随机字符串
System.out.println(result);
}
}
测试: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. *
*************************************************************************/
import java.util.Random;
import java.util.Scanner;
public class Sizeyunsuan
{
public static void main(String[]args)
{
int a=(int)(Math.random()*100);
int b=(int)(Math.random()*4);
int c=(int)(Math.random()*100);
System.out.println("请输入您想要多少道题");
Scanner in=new Scanner(System.in);
int j;
j=in.nextInt();
System.out.println("请输入您想几个一排:");
int k;
k=in.nextInt();
for(int i=1;i<=j;i++)
{
b=(int)(Math.random()*4);//产生[0,4)的随机数,用此来控制加减乘除,0代表加,依次类推。
if(b==0)//实现两数相加不超过100
{
do
{
a=(int)(Math.random()*100);
c=(int)(Math.random()*100);
if(a+c<100)
{
if(i%k==0)
{
System.out.println("第"+i+"道题:"+a+"+"+c+"="+" ");
break;
}
else
{
System.out.print("第"+i+"道题:"+a+"+"+c+"="+" ");
break;
}
}
}while(a>=0);
}
if(b==1)//实现两数相减大于0
{
do
{
a=(int)(Math.random()*100);
c=(int)(Math.random()*100);
if(a>c)
{
if(i%k==0)
{
System.out.println("第"+i+"道题:"+a+"-"+c+"="+" ");
break;
}
else
{
System.out.print("第"+i+"道题:"+a+"-"+c+"="+" ");
break;
}
}
}while(a>=0);
}
if(b==2)//实现两数相乘不超过100
{
do
{
a=(int)(Math.random()*100);
c=(int)(Math.random()*100);
if(a*c<100)
{
if(i%k==0)
{
System.out.println("第"+i+"道题:"+a+"*"+c+"="+" ");
break;
}
else
{
System.out.print("第"+i+"道题:"+a+"*"+c+"="+" ");
break;
}
}
}while(a>=0);
}
if(b==3)//解决整除问题
{
do
{
a=(int)(Math.random()*100);
c=(int)(Math.random()*100);
if(c!=0&&a%c==0)
{
if(i%k==0)
{
System.out.println("第"+i+"道题:"+a+"/"+c+"="+" ");
break;
}
else
{
System.out.print("第"+i+"道题:"+a+"/"+c+"="+" ");
break;
}
}
}while(a>=0);
}
}
}
}