return commonDivisor(N,M%N);
}
}
最小公倍数和最大公约数:
import java.util.Scanner;
public class CandC
{
//下面的方法是求出最大公约数
public static int gcd(int m, int n)
{
while (true)
{
if ((m = m % n) == 0)
return n;
if ((n = n % m) == 0)
return m;
}
}
public static void main(String args[]) throws Exception
{
//取得输入值
//Scanner chin = new Scanner(System.in);
//int a = chin.nextInt(), b = chin.nextInt();
int a=23; int b=32;
int c = gcd(a, b);
System.out.println("最小公倍数:" + a * b / c + "\n最大公约数:" + c);
}
}
【程序 7】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用 while 语句,条件为输入的字符不为 '\n '.
import java.util.Scanner;
public class ex7 {
public static void main(String args[])
{
System.out.println("请输入字符串:");
Scanner scan=new Scanner(System.in);
String str=scan.next();
String E1="[\u4e00-\u9fa5]";
String E2="[a-zA-Z]";
int countH=0;
int countE=0;
char[] arrChar=str.toCharArray();
String[] arrStr=new String[arrChar.length];
for (int i=0;i<arrChar.length ;i++ )
{
arrStr[i]=String.valueOf(arrChar[i]);
}
for (String i: arrStr )
{
- 1
- 2
- 3
- 4
- 5
- 6
前往页