JAVA编程题全集(100题及答案)

内容发布更新时间 : 2025/6/15 15:51:09星期一 下面是文章的全部内容请认真阅读。

int n = s.nextInt(); int k=2;

while(k <= n) {

else k++; } } } 【程序5】

题目:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。 import java.util.*; public class lianxi05 {

public static void main(String[] args) { int x;

char grade;

Scanner s = new Scanner(System.in);

x = s.nextInt(); grade = x >= 90 ? 'A' : x >= 60 ? 'B' :'C'; } }

【程序6】

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

/**在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。* /

import java.util.*;

public class lianxi06 {

public static void main(String[] args) { int a ,b,m;

Scanner s = new Scanner(System.in);

a = s.nextInt();

b = s.nextInt();

deff cd = new deff(); m = cd.deff(a,b); int n = a * b / m; } }

class deff{

public int deff(int x, int y) { int t;

if(x < y) { t = x; x = y; y = t; }

while(y != 0) { if(x == y) return x; else {

int k = x % y; x = y; y = k; } }

return x; } }

【程序7】

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 import java.util.*; public class lianxi07 {

public static void main(String[] args) { int digital = 0; int character = 0; int other = 0; int blank = 0;

char[] ch = null;

Scanner sc = new Scanner(System.in); String s = sc.nextLine(); ch = s.toCharArray();

for(int i=0; i

>>展开全文<<
12@gma联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi