本人7.16基础班,上12天课了。以下纯手打,第八题还没教不会,其他应该没问题,跪求技术分!!!!
/* 01 判断101-200之间有多少个素数,并输出所有素数。?*/
package cn.itcast;
public class GetPrimeNumber {
public static void main(String[] args) {
int count = 0;
for (int i = 101; i < 201; i++) {
for (int j = 2; j <i ; j++) {
if (i%j==0 ) {
break ;
}
if(j==(i-1))
{
System.out.print(i+",");
count++;
}
}
}
System.out.println();
System.out.println(count);
}
}
/*02、“将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。*/
package cn.itcast;
import java.util.Scanner;
public class FenJie {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数值");
int num = sc.nextInt();
int temp =num;
System.out.print(num+"=");
while(temp>1){
for (int i = 2; i < num ; i++) {
if (temp%i==0) {
temp=temp/i;
System.out.print(i);
break;
}
}
System.out.print("*");
}
}
}
/** 03、输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。*/
package cn.itcast;
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String str = sc.nextLine();
int numbercount = 0;
int abccount=0;
int blankcount=0;
int othercount=0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i)>='0'&& str.charAt(i)<='9') {
numbercount++;
}else if (str.charAt(i)==' ') {
blankcount++;
}else if ((str.charAt(i)>='A'&&str.charAt(i)<='Z')||(str.charAt(i)>='a'&&str.charAt(i)<='z')) {
abccount++;
}else{
othercount++;
}
}
System.out.println("其中英文有"+abccount+"个,其中数字有"+numbercount+"个,其中空格有"+blankcount+"个,其他字符有"+othercount+"个");
}
}
/*04 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。*/
package cn.itcast;
import java.util.Scanner;
public class Add4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入1-9之间的数字:");
int num = sc.nextInt();
System.out.println("请输入相加次数:");
int time = sc.nextInt();
int value = num;
int temp = num;
for (int i = 1; i < time; i++) {
temp += haha(num, i);
value += temp;
}
System.out.println(value);
}
public static int haha(int num,int time){
int k = num;
for (int i = 1; i <=time; i++) {
k*=10;
}
return k;
}
}
/** 05、有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?*/
package cn.itcast;
public class Q5 {
public static void main(String[] args) {
int count = 0;
for (int x = 122; x < 433; x++) {
int a, b, c;
a = x / 100;
b = x % 100 / 10;
c = x % 10;
if (a != b && a != c && b != c && a >= 1 && a <= 4 && b >= 1
&& b <= 4 && c >= 1 && c <= 4) {
count++;
System.out.print(x + ",");
}
}
System.out.println();
System.out.println(count);
}
}
/*06、给一个不多于5位的正整数。要求:一、它是几位数,二、逆序打印出各位数字。*/
package cn.itcast;
import java.util.Scanner;
public class Q6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個不多於5的正整數:");
int num = sc.nextInt();
int temp = num;
int count = 0;
while (temp > 0) {
temp = temp / 10;
count++;
}
System.out.println("它是" + count + "位數");
for (int j = 1; j <= count; j++) {
int haha= num%Shi(j);
haha=haha/Shi(j-1);
System.out.print(haha);
}
}
public static int Shi(int y) {
int value = 1;
for (int i = 1; i <= y; i++) {
value *= 10;
}
return value;
}
}
/**07、输入三个整数x、y、z,把这三个数由小到大输出。*/
package cn.itcast;
import java.util.Scanner;
public class Q7 {
public static void main(String[] args) {
int[] arr = new int[3];
Scanner sc = new Scanner(System.in);
System.out.println("请输入三个整数:");
arr[0] = sc.nextInt();
arr[1] = sc.nextInt();
arr[2] = sc.nextInt();
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length-1; j++) {
if(arr[j]>arr[j+1]){
int temp =arr[j+1];
arr[j+1] =arr[j];
arr[j]=temp;
}
}
}
print(arr);
}
public static void print(int[] arr){
for (int i = 0; i < arr.length-1; i++) {
System.out.print(arr[i]+",");
}
System.out.println(arr[arr.length-1]);
}
}
/**09、一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 */
package cn.itcast;
import java.util.Scanner;
public class Q9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个五位数");
int num = sc.nextInt();
if (num / Shi(4) == num % 10
&& num % Shi(4) / Shi(3) == num % Shi(2) / 10) {
System.out.println(num + "它是一个回文数");
} else {
System.out.println(num + "它不是一个回文数");
}
}
public static int Shi(int y) {
int value = 1;
for (int i = 1; i <= y; i++) {
value *= 10;
}
return value;
}
}
/**10、 将一个数组逆序输出,(不能使用API的方法)。*/
package cn.itcast;
public class Q10 {
public static void main(String[] args) {
int[] arr = { 12, 3, 6, 2, 8, 23, 1, 9,56,78 };
for (int i = arr.length-1; i >0; i--) {
for (int j = 0; j < i ; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
print(arr);
}
public static void print(int[] arr){
for (int i = arr.length-1; i >0; i--) {
System.out.print(arr[i]+",");
}
System.out.println(arr[0]);
}
}
|