问两道简单的Java题目!高分求教

1.Thegreatestcommondivisor(GCD)oftwointegersisthelargestintegerthatevenlydividseachof... 1.The greatest common divisor(GCD)of two integers is the largest integer that evenly divids each of the two numbers.White a method gcd that returns the greatest common divisor of two integers.Incorporate the method into an application that reads two values from the user and displays the result.
大意是要求用户输入两个整数,然后计算出它们的最大公约数,然后把结果显示出来给用户看。

2.The use of computers in education is referred to as computer-assisted instruction(CAI).One problem that develops in CAI environments is student fatigue.This problem can be eliminated by varying the computer's responses to hold the student's attention.the various comments are displayed for each correct answer and each incorrect answer as follows:
Responses to a correct answer:

Very good!
Excellent!
Nice work!
keep up the good work!

Responses to an incorrect answer:

No.Please try again.
Wrong.Try once more.
Don't give up!
No.keep trying.

Use random-number generation to choose a number from 1 to 4 that will be used to select an appropriate response to each answer.Use a switch statement to issue the responses.
大意是设计一些问题,然后让用户输入答案,如果答案正确,则随机显示上面的鼓励信息,否则,则随机显示上面的错误信息,再让用户重新答题,直到这题的答案正确后显示鼓励信息就接着做下一题。具体请看英文原题。要用到随机数和switch语句。至于提出给用户回答的问题就大约4-6道,全部是简单乘法题目,如“How much is 6 times 7?”
请大虾们指导,到时我会再加分。
展开
 我来答
jasochn
2006-12-14
知道答主
回答量:82
采纳率:0%
帮助的人:72.8万
展开全部
second question:

import java.util.Random;
import java.util.Scanner;

public class rantest
{
public static void main( String args[] )
{
Random randomNumbers = new Random();
Scanner input = new Scanner(System.in);
int a1;
int a2;
int a3;
int a4;
int a5;
int a6;
int k;

System.out.println("Now begin the test!");
System.out.println("This test have 6 questions.Please enter your answer follow the question!");

do
{
System.out.println("Question 1:");
System.out.println("How much is 6 times 7?");
a1 = input.nextInt();

if ( a1 != 42 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("No.Please try again.");
break;

case 2:
System.out.println("Wrong.Try once more.");
break;

case 3:
System.out.println("Don't give up!");
break;

case 4:
System.out.println("No.keep trying.");
break;
}
}

if ( a1 == 42 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("Very good!");
break;

case 2:
System.out.println("Excellent!");
break;

case 3:
System.out.println("Nice work!");
break;

case 4:
System.out.println("keep up the good work!");
break;
}
break;
}
}
while(true);
System.out.println();

do
{
System.out.println("Question 2:");
System.out.println("How much is 9 times 9?");
a2 = input.nextInt();

if ( a2 != 81 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("No.Please try again.");
break;

case 2:
System.out.println("Wrong.Try once more.");
break;

case 3:
System.out.println("Don't give up!");
break;

case 4:
System.out.println("No.keep trying.");
break;
}
}

if ( a2 == 81 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("Very good!");
break;

case 2:
System.out.println("Excellent!");
break;

case 3:
System.out.println("Nice work!");
break;

case 4:
System.out.println("keep up the good work!");
break;
}
break;
}
}
while(true);
System.out.println();

do
{
System.out.println("Question 3:");
System.out.println("How much is 15 times 17?");
a3 = input.nextInt();

if ( a3 != 255 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("No.Please try again.");
break;

case 2:
System.out.println("Wrong.Try once more.");
break;

case 3:
System.out.println("Don't give up!");
break;

case 4:
System.out.println("No.keep trying.");
break;
}
}

if ( a3 == 255 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("Very good!");
break;

case 2:
System.out.println("Excellent!");
break;

case 3:
System.out.println("Nice work!");
break;

case 4:
System.out.println("keep up the good work!");
break;
}
break;
}
}
while(true);
System.out.println();

do
{
System.out.println("Question 4:");
System.out.println("How much is 20 times 20?");
a4 = input.nextInt();

if ( a4 != 400 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("No.Please try again.");
break;

case 2:
System.out.println("Wrong.Try once more.");
break;

case 3:
System.out.println("Don't give up!");
break;

case 4:
System.out.println("No.keep trying.");
break;
}
}

if ( a4 == 400 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("Very good!");
break;

case 2:
System.out.println("Excellent!");
break;

case 3:
System.out.println("Nice work!");
break;

case 4:
System.out.println("keep up the good work!");
break;
}
break;
}
}
while(true);
System.out.println();

do
{
System.out.println("Question 5:");
System.out.println("How much is 425 times 278?");
a5 = input.nextInt();

if ( a5 != 118150 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("No.Please try again.");
break;

case 2:
System.out.println("Wrong.Try once more.");
break;

case 3:
System.out.println("Don't give up!");
break;

case 4:
System.out.println("No.keep trying.");
break;
}
}

if ( a5 == 118150 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("Very good!");
break;

case 2:
System.out.println("Excellent!");
break;

case 3:
System.out.println("Nice work!");
break;

case 4:
System.out.println("keep up the good work!");
break;
}
break;
}
}
while(true);
System.out.println();

do
{
System.out.println("Question 6:");
System.out.println("How much is 0 times 878?");
a6 = input.nextInt();

if ( a6 != 0 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("No.Please try again.");
break;

case 2:
System.out.println("Wrong.Try once more.");
break;

case 3:
System.out.println("Don't give up!");
break;

case 4:
System.out.println("No.keep trying.");
break;
}
}

if ( a6 == 0 )
{
k = 1 + randomNumbers.nextInt(4);

switch( k )
{
case 1:
System.out.println("Very good!");
break;

case 2:
System.out.println("Excellent!");
break;

case 3:
System.out.println("Nice work!");
break;

case 4:
System.out.println("keep up the good work!");
break;
}
break;
}
}
while(true);
System.out.println();

System.out.println("Well done!");
System.out.println("The test is finish!");
}
}
百度网友29afc6726
2006-12-12 · TA获得超过342个赞
知道小有建树答主
回答量:456
采纳率:0%
帮助的人:283万
展开全部
第一题的答案:
import java.io.*;

public class gcd
{
public static void main(String argc[])
{
int temp,m=15,n=20;

String chan=null;
try
{
System.out.print("请输入数字1:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
chan=br.readLine();
m=Integer.parseInt(chan);

System.out.print("请输入数字2:");
chan=br.readLine();
n=Integer.parseInt(chan);
}
catch(IOException e)
{}

// 小数放在m中
if(n<m)
{
temp=n;
n=m;
m=temp;
}

while(m!=0)
{
temp=n%m;
n=m;
m=temp;
}
System.out.println("最大公约数为:"+n);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
13037376
2006-12-12 · TA获得超过1372个赞
知道小有建树答主
回答量:1198
采纳率:0%
帮助的人:1165万
展开全部
最大公约数:
1、欧几里德算法和扩展欧几里德算法

欧几里德算法
欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。其计算原理依赖于下面的定理:

定理:gcd(a,b) = gcd(b,a mod b)

证明:a可以表示成a = kb + r,则r = a mod b
假设d是a,b的一个公约数,则有
d|a, d|b,而r = a - kb,因此d|r
因此d是(b,a mod b)的公约数

假设d 是(b,a mod b)的公约数,则
d | b , d |r ,但是a = kb +r
因此d也是(a,b)的公约数

因此(a,b)和(b,a mod b)的公约数是一样的,其最大公约数也必然相等,得证

欧几里德算法就是根据这个原理来做的,其算法用C++语言描述为:

void swap(int & a, int & b)
{
int c = a;
a = b;
b = c;
}
int gcd(int a,int b)
{
if(0 == a )
{
return b;
}
if( 0 == b)
{
return a;
}
if(a > b)
{
swap(a,b);
}
int c;
for(c = a % b ; c > 0 ; c = a % b)
{
a = b;
b = c;
}
return b;
}

2、Stein算法
欧几里德算法是计算两个数最大公约数的传统算法,他无论从理论还是从效率上都是很好的。但是他有一个致命的缺陷,这个缺陷只有在大素数时才会显现出来。

考虑现在的硬件平台,一般整数最多也就是64位,对于这样的整数,计算两个数之间的模是很简单的。对于字长为32位的平台,计算两个不超过32位的整数的模,只需要一个指令周期,而计算64位以下的整数模,也不过几个周期而已。但是对于更大的素数,这样的计算过程就不得不由用户来设计,为了计算两个超过64位的整数的模,用户也许不得不采用类似于多位数除法手算过程中的试商法,这个过程不但复杂,而且消耗了很多CPU时间。对于现代密码算法,要求计算128位以上的素数的情况比比皆是,设计这样的程序迫切希望能够抛弃除法和取模。

Stein算法由J. Stein 1961年提出,这个方法也是计算两个数的最大公约数。和欧几里德算法 算法不同的是,Stein算法只有整数的移位和加减法,这对于程序设计者是一个福音。

为了说明Stein算法的正确性,首先必须注意到以下结论:

gcd(a,a) = a,也就是一个数和他自身的公约数是其自身
gcd(ka,kb) = k gcd(a,b),也就是最大公约数运算和倍乘运算可以交换,特殊的,当k=2时,说明两个偶数的最大公约数必然能被2整除

C++/java 实现
// c++/java stein 算法
int gcd(int a,int b){
if(a<b)//arrange so that a>b{
int temp = a;
a = b;
b=temp;
}
if(0==b)//the base case
return a;
if(a%2==0 && b%2 ==0)//a and b are even
return 2*gcd(a/2,b/2);
if ( a%2 == 0)// only a is even
return gcd(a/2,b);
if ( b%2==0 )// only b is even
return gcd(a,b/2);

return gcd((a+b)/2,(a-b)/2);// a and b are odd

}

2题很简单 代码有很长...就说说想法
把答题后返回的信息编好号0-3(正确信息,错误信息都编),每次答完问题都生成0-3范围内的随机数, int k = random.nextInt(3); 再判断回答的对错,然后从不同的地方把信息取出来,再根据回答的对错判断是继续循环本次答题还是跳出 回答下1题
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式