1.
public class Date {
private int month;
private int day;
private int year;
public Date( int theMonth, int theDay, int theYear )
{
month = checkMonth( theMonth );
year = theYear;
day = checkDay( theDay );
System.out.println( "Date object constructor for date " +
toDateString() );
}
private int checkMonth( int testMonth )
{
if ( testMonth > 0 && testMonth <= 12 )
return testMonth;
else {
System.out.println( "Invalid month (" + testMonth +
") set to 1." );
return 1;
}
}
private int checkDay( int testDay )
{
int daysPerMonth[] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
System.out.println( "Invalid day (" + testDay + ") set to 1." );
return 1;
}
public String toDateString()
{
return month + "/" + day + "/" + year;
}
}
3.
方法1:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class No1_Min extends JApplet {
JButton button;
String str="抛硬币:";
public void init(){
button=new JButton("Toss");
Container container=getContentPane();
container.setLayout(new FlowLayout());
container.add(button);
button.addActionListener(
new ActionListener(){
int front=0,back=0;
public void actionPerformed(ActionEvent e){
if (Toss()) front++;
else back++;
str="正面"+front+"次 "+"负面"+back+"次";
showStatus(str);
}//end actionPerformed
}//end ActionListener
);
}//end init
boolean Toss(){
int Num;
Num=1+(int)(Math.random()*2);
if(Num==1)return true;
else return false;
}//end Toss
}//end coin
方法2:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class No1_Min extends JApplet implements ActionListener{
JButton button;
String str;
int front=0,back=0;
public void init(){
button=new JButton("Toss");
Container container=getContentPane();
container.setLayout(new FlowLayout());
container.add(button);
button.addActionListener(this);
}//end init
public void actionPerformed(ActionEvent e){
if (Toss()) front++;
else back++;
str="正面"+front+"次 "+"负面"+back+"次";
showStatus(str);
}//end actionPerformed
boolean Toss(){
int Num;
Num=1+(int)(Math.random()*2);
if(Num==1)return true;
else return false;
}//end Toss
}//end coin
温馨提示:答案为网友推荐,仅供参考