⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 百鸡问题参考答案.txt

📁 一百块钱买一百只鸡
💻 TXT
字号:
1.编写一个应用程序,找出几个整数中的最小值。假定用户输入的第一个数为数值的个数。用JoptionPane类来输入数值,要对输入的数据进行是否为数值的的判断。

2.计算1到5之间的各个整数的阶乘,结果以表格的形式放在JTextArea中,并用消息对话框显示该JTextArea.

3.编写一个applet,读取5个介于1-30之间的数。每读一个数,程序都要画出一行含有相应数目的连续星号。例如,读入7,程序显示*******。

4.百鸡问题的例子:公鸡7元一只,母鸡5元一只,小鸡一元三只,100元钱买100只鸡,若公鸡、母鸡和小鸡都必须有,则公鸡、母鸡和小鸡各应买几只? 用continue语句实现,结果以表格的形式放在JTextArea中,并用消息对话框显示该JTextArea.


1.参考答案

import javax.swing.*;
public class No1_Min {
	public static void main(String[] args) {
		int intNumber;
		int max=0;
		int tempNumber;
		String input;
		while(true){
		input=JOptionPane.showInputDialog("请输入比较的整数个数:\n");
		if ( !(input.matches("\\d*"))){                             //判断是否为数字字符。
			JOptionPane.showMessageDialog(null, "type wrong,retype");
		}else break;		
		}
		
		intNumber=Integer.parseInt(input);
		
		for(int i=1;i<=intNumber;i++){
			String temp=JOptionPane.showInputDialog("第"+i+"个数是:\n");
			if ( !(temp.matches("\\d*"))){
				JOptionPane.showMessageDialog(null, "type wrong,retype");
				i--;
				continue;
			}
			tempNumber=Integer.parseInt(temp);
			if (tempNumber>=max)
				max=tempNumber;
		}
		JOptionPane.showMessageDialog(null, "最大的数是"+max);
	}
}

2.计算1到5之间的各个整数的阶乘,结果以表格的形式放在JTextArea中,并用消息对话框显示该JTextArea.
参考答案:
public class Multify{	
	  int fm (int iNum){
		if(iNum==1)return 1;
		else return iNum*fm(iNum-1);
	}	
	public static void main(String args[]){
                Multify a=new Multify();
                int n=5;
		a.fm(n);
		System.out.println("Outcome:"+n);		
	}//end of main
}


3.
/*编写一个applet,读取5个介于1-30之间的数。每读一个数,程序都要画出一行含有相应数目的连续星号。
 例如,读入7,程序显示*******。
*/
import javax.swing.*;
import java.awt.*;
public class No1_Min extends JApplet{
	int iArray[]=new int[5];
	
	public void init(){
		int i=0;
		while(i<5){
			try{ 					   
			   iArray[i]=Integer.parseInt( JOptionPane.showInputDialog("请输入一个整数:"));
			   if(iArray[i]>30||iArray[i]<1) throw new Exception();
		    }catch(Exception e){
			       JOptionPane.showMessageDialog(null,"输入错误,请重新输入一个整数:");
			       i--;
		     }
		    i++;
		}//end of while
	}//the end of init
	
	public void paint(Graphics g){
		super.paint(g);
		for(int i =0; i<5;i++){
			for(int j=0;j<iArray[i];j++)
			  g.drawString("*", j*20+100, i*20+100);
		}
		
	}//end of paint
	
}// the end of the class


4.参考答案
import javax.swing.*;
public class No1_Min {
public static void main( String args[] ){
	
JTextArea outputTextArea = new JTextArea(17,20);
outputTextArea.setText( "公鸡数\t 母鸡数\t 小鸡数\n" );
JScrollPane scroller = new  JScrollPane(outputTextArea);
int  cock;
int  hen;
int chicken;
for (cock=1;cock<=13;cock++){
	for(hen=1;hen<=18;hen++){
	   if(7*cock+5*hen+(100-cock-hen)/3!=100)	continue;
       if((100-cock-hen)%3!=0) continue;
       chicken=100-cock-hen;
      outputTextArea.append( ""+cock + "\t" + hen + "\t"+chicken+"\n" );
    }
}
 
JOptionPane.showMessageDialog( null, scroller,"百鸡问题", JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}

	
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -