一些java题
1、 使用Java语言编写两个方法,分别实现求两个整数的最大公约数和最小公倍数。在main()方法里面进行测试。要求是:从控制台进行两个整数的输入。2、 编写一个猜数的应用程序,用随机函数产生一个10到99的随机数。根据猜中使用的次数X计算得分。每次猜完给出是大于还是小于的提示。注:得分的计算办法:110―X*10;
3、 利用随机函数产生10道两位数的加法题,判断用户的得分。程序运行界面为:
13+35=? xx
34+27=? xx
…………..
你的得分是:80分
4、 从键盘输入一个长整数,求其各位数字之和。
5、 编写根据三条边求三角形面积的方法。利用该方法求以下两个三角形面积。
a) 三条边分别为:3,4,5;
b) 三条边分别为:5.3,6.2,8;
请高手给指点指点!
搜索更多相关的解决方案:
java
----------------解决方案--------------------------------------------------------
1、 使用Java语言编写两个方法,分别实现求两个整数的最大公约数和最小公倍数。在main()方法里面进行测试。要求是:从控制台进行两个整数的输入。
程序代码:
import java.util.*;
public class Ex1 {
public static void gongYue(int m, int n) {
if (m > n) {
int h = m;
m = n;
n = h;
}
if (n % m == 0) {
System.out.println("最大公约数为:" + m);
} else {
for (int i = n / 2; i > 1; i--) {
if (m % i == 0 && n % i == 0) {
System.out.println("最大公约数为:" + i);
break;
}
}
}
}
public static void gongBei(int m, int n) {
if (m > n) {
int h = m;
m = n;
n = h;
}
for (int j = n; j <= m* n; j++) {
if (j % m == 0 && j % n == 0) {
System.out.println("最小公倍数:" + j);
break;
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("x=");
int x = scan.nextInt();
System.out.print("y=");
int y = scan.nextInt();
gongYue(x, y);
gongBei(x, y);
}
}
public class Ex1 {
public static void gongYue(int m, int n) {
if (m > n) {
int h = m;
m = n;
n = h;
}
if (n % m == 0) {
System.out.println("最大公约数为:" + m);
} else {
for (int i = n / 2; i > 1; i--) {
if (m % i == 0 && n % i == 0) {
System.out.println("最大公约数为:" + i);
break;
}
}
}
}
public static void gongBei(int m, int n) {
if (m > n) {
int h = m;
m = n;
n = h;
}
for (int j = n; j <= m* n; j++) {
if (j % m == 0 && j % n == 0) {
System.out.println("最小公倍数:" + j);
break;
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("x=");
int x = scan.nextInt();
System.out.print("y=");
int y = scan.nextInt();
gongYue(x, y);
gongBei(x, y);
}
}
[ 本帖最后由 lampeter123 于 2010-4-17 12:06 编辑 ]
----------------解决方案--------------------------------------------------------
程序代码:
import java.util.Random;
import java.util.Scanner;
public class GuessNum {
public static final int EQUALS=0;
public static final int GREATER_THAN=1;
public static final int LITTLE_THAN=2;
int count=0;
private int generateNum(){
Random random = new Random();
return 10+random.nextInt(90);
}
private int getTotal(){
return 110-(count+1)*10;
}
public int compare(int guess, int random){
if(guess==random){
System.out.println("恭喜你,你猜对了");
System.out.println("你猜对了. 你的得分是:"+getTotal());
return EQUALS;
}else if(guess<random){
count++;
System.out.println("你已经猜了"+count+"次了,你的数小了");
return LITTLE_THAN;
}else{
count++;
System.out.println("你已经猜了"+count+"次了,你的数大了");
return GREATER_THAN;
}
}
public static void main(String[] args){
GuessNum gn = new GuessNum();
Scanner scan = new Scanner(System.in);
int num = gn.generateNum();
int guess;
do{
System.out.println("请输入一个10到99之间的数字");
guess = scan.nextInt();
}
while(gn.compare(guess, num)!=GuessNum.EQUALS);
}
}
import java.util.Scanner;
public class GuessNum {
public static final int EQUALS=0;
public static final int GREATER_THAN=1;
public static final int LITTLE_THAN=2;
int count=0;
private int generateNum(){
Random random = new Random();
return 10+random.nextInt(90);
}
private int getTotal(){
return 110-(count+1)*10;
}
public int compare(int guess, int random){
if(guess==random){
System.out.println("恭喜你,你猜对了");
System.out.println("你猜对了. 你的得分是:"+getTotal());
return EQUALS;
}else if(guess<random){
count++;
System.out.println("你已经猜了"+count+"次了,你的数小了");
return LITTLE_THAN;
}else{
count++;
System.out.println("你已经猜了"+count+"次了,你的数大了");
return GREATER_THAN;
}
}
public static void main(String[] args){
GuessNum gn = new GuessNum();
Scanner scan = new Scanner(System.in);
int num = gn.generateNum();
int guess;
do{
System.out.println("请输入一个10到99之间的数字");
guess = scan.nextInt();
}
while(gn.compare(guess, num)!=GuessNum.EQUALS);
}
}
----------------解决方案--------------------------------------------------------
第三题:
程序代码:
import java.util.Random;
import java.util.Scanner;
public class AddTest {
private static int generateNum(){
Random random = new Random();
return 10+random.nextInt(90);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int answer;
int sum=0;
Scanner scan = new Scanner(System.in);
for(int i=0;i<10;i++){
int x = generateNum();
int y = generateNum();
answer = x+y;
System.out.print(x+" +"+y+" = ");
int myanswer = scan.nextInt();
if(myanswer==answer)
sum+=10;
}
System.out.println("***************************");
System.out.println("你的得分是:"+sum);
}
}
import java.util.Scanner;
public class AddTest {
private static int generateNum(){
Random random = new Random();
return 10+random.nextInt(90);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int answer;
int sum=0;
Scanner scan = new Scanner(System.in);
for(int i=0;i<10;i++){
int x = generateNum();
int y = generateNum();
answer = x+y;
System.out.print(x+" +"+y+" = ");
int myanswer = scan.nextInt();
if(myanswer==answer)
sum+=10;
}
System.out.println("***************************");
System.out.println("你的得分是:"+sum);
}
}
----------------解决方案--------------------------------------------------------
第四题
程序代码:
Scanner scan = new Scanner(System.in);
long lnum = scan.nextLong();
int sum = 0;
while(lnum!=0){
sum=sum+(int)(lnum%10);
lnum=lnum/10;
}
System.out.println(sum);
long lnum = scan.nextLong();
int sum = 0;
while(lnum!=0){
sum=sum+(int)(lnum%10);
lnum=lnum/10;
}
System.out.println(sum);
----------------解决方案--------------------------------------------------------
最后一个也太简单了,直接利用公式就行了
p=(a+b+c)/2
三角形的面积就是p(p-a)(p-b)(p-c)的开方
----------------解决方案--------------------------------------------------------
第一道题: //后面的一一道来啊 别急,因为我也是新手,相互学习嘛!嗬嗬嗬嗬
import javax.swing.JOptionPane;
public class CommonDivisor {
public static void main(String[] args) {
String number1 = JOptionPane.showInputDialog("Enter an integer!");
String number2 = JOptionPane.showInputDialog("Enter an integer!");
int n = Integer.parseInt(number1);
int m = Integer.parseInt(number2);
int i = divisor(n, m);
int j = multiple(n, m);
String output = "The common divisor is " + i + "\n"
+ "The common multiple is " + j;
JOptionPane.showMessageDialog(null, output);
}
public static int divisor(int num1, int num2) {
int gcd = 1;
int k = 1;
while (k <= num1 && k <= num2) {
if (num1 % k == 0 && num2 % k == 0)
gcd = k;
k++;
}
return gcd;
}
public static int multiple(int num3, int num4) {
int gcd1 = 1;
//int x = 1;
for (int x =num3 * num4; x >=1; x--) {
if (x % num3 == 0 && x % num4 == 0){
gcd1 = x;
//return gcd1;
}
}
return gcd1;}
}
----------------解决方案--------------------------------------------------------
第二道:
import javax.swing.JOptionPane;
public class guess {
public static void main(String[] args) {
String output = "";
int n = (int) (10 + 90 * Math.random());
for (int i = 1; i <= 100; i++) {
String number = JOptionPane
.showInputDialog("Please input the answer !");
int m = Integer.parseInt(number);
int sum = 0;
int count = 0;
count++;
sum = 110 - count * 10;
if (m <= 0 || m >= 100) {
output = "Please an integer 10-99 !";
} else if (m < n) {
output = "输入的值小于实际值!";
} else if (m > n) {
output = "输入的值大于实际值!";
} else {
output = "恭喜您答对了!您的最终得分为: " + sum;
JOptionPane.showMessageDialog(null, output);
break;
}
JOptionPane.showMessageDialog(null, output);
}
}
}
----------------解决方案--------------------------------------------------------
第五道题:
import javax.swing.JOptionPane;
public class He {
public static void main(String[] args) {
double arr[] = new double[3];
String output = "";
for (int x = 0; x <= 10000; x++) {
for (int i = 1; i <= 3; i++) {
String number = JOptionPane.showInputDialog("请输入三角形的第" + i
+ "条边!");
double n = Double.parseDouble(number);
arr[i - 1] = n;
}
if (arr[0] + arr[1] <= arr[2] || arr[0] + arr[1] <= arr[2]
|| arr[0] + arr[1] <= arr[2] || arr[0] == 0 || arr[1] == 0
|| arr[2] == 0) {
output = "数据错误,请输入正确的值!";
JOptionPane.showMessageDialog(null, output);
continue;
} else {
double area = 0;
double P = 0;
P = (arr[0] + arr[1] + arr[2]) / 2;
area = Math.pow(P * (P - arr[0]) * (P - arr[1]) * (P - arr[2]),
1.0 / 2);
output = "三角形的面积为: " + area;
JOptionPane.showMessageDialog(null, output);
break;
}
}
}
}
----------------解决方案--------------------------------------------------------
第三道题:
public class EverySum {
public static void main(String[] args) {
int i = 0;
String output = "";
while (i <= 10) {
int m = (int) (Math.random() * 90 + 10);
int n = (int) (Math.random() * 90 + 10);
i++;
output = "\n" + n + "+" + m + "=? " + (int) (m + n);
JOptionPane.showMessageDialog(null, output);
}
}
}
----------------解决方案--------------------------------------------------------