程序小白--一些常见面试小程序

一:如何实现一行字符串的倒叙输出


public class Daoxun {
        public static void main(String[] args) {
String s;
Scanner in =new Scanner(System.in);
System.out.println("请输入一行字符串");
s=in.nextLine();
StringBuffer b=new StringBuffer(s);
System.out.println(b.reverse().toString());

/*System.out.println("你输入的为:"+s);
char a[]=s.toCharArray();
System.out.print("倒序输出的为:");
for(int i=a.length-1;i>=0;i--){
System.out.print(a[i]);
}*/
}
}


二:如何将不定量的数字排序输出

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;


public class Demo1 {
public static void main(String[] args) {
System.out.println("请输入数字,当输入q的时候退出");
String s="";
ArrayList list = new ArrayList<>();
do{
Scanner in = new Scanner (System.in);
s = in.nextLine();
if("q".equals(s)){
break;

}else{
list.add(Integer.parseInt(s));
}
}while(!"q".equals(s));
int a[] = new int[list.size()];
for(int i=0;ia[i]=list.get(i);
}
Arrays.sort(a);
for(int x:a){
System.out.print(x+" ");
}
}



}


三:九九乘法表

public class jiujiu {
public static void main(String[] args) {
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();
}

}
}


4.将一行字符串的每一个单词首字母变大写

package com.xiaowei.homewoke.model;


import java.util.Scanner;


public class Daxie {
public static void main(String[] args) {
System.out.println("请输入一行英文字符串:");
Scanner in = new Scanner(System.in);
String a = in.nextLine();
    String []b=a.split(" ");  //以空格为分割符,将a打散放入字符数组b中

for(int i=0;ib[i]=b[i].substring(0,1).toUpperCase()+b[i].substring(1);   //检索每个元素的第一个字母将其变为大写,再将其后续的字母加入进来
System.out.print(b[i]+" ");
}


5.输入一个N判断1到N的中间有多少个质数,并输出



方法一:

package com.xiaowei.homewoke.model;


import java.util.Scanner;


public class Deno {
public static void main(String[] args){ 
 
        int num=0; 
        int end; 
        Scanner in = new Scanner(System.in); 
        System.out.println( "请输入数值:"); 
      
        end = in.nextInt(); 
       System.out.println("1到"+end+"的素数为:");
         for(int i =2;i<=end;i++){ 
         
            if(isPrime(i)){ 
            num++;
            System.out.print(i + " "); 
            } 
            
        } 
         System.out.println();
         System.out.println("一共有"+num+"个素数");
    }


private static boolean isPrime(int i) {
for (int j = 2; j < i; j++) {
           if ((i % j) == 0) { 
               return false; 
           } 
       } 
       return true;


方法二:

package lixi;


import java.util.Scanner;


public class zhishu {
public static void main(String[] args) {
int num = 0;
boolean flag = true;
System.out.println("请输入一个数字:");
Scanner in = new Scanner(System.in);
int a = in.nextInt();


for (int i = 2; i <= a; i++) {
// b=a/2;
for (int j = 2; j < i; j++) { // for(int j=2;j<=b;j++){
if (i % j == 0) {
flag = false;
break;
}
}


if (flag) {
num++;
System.out.println(i);


}
flag = true;
}
System.out.println();
System.out.println("總共有" + num + "素數");
}
}




6.输入两个数判断两个数的最小公倍数和最大公约数

方法一:

package com.xiaowei.homewoke.model;


import java.util.Scanner;


public class Main
{
public static int yue(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 in=new Scanner(System.in);
int a=in.nextInt(),b=in.nextInt();
int c = yue(a,b);
System.out.println("最小公倍数:" + a * b / c + "\n最大公约数:" + c);
}
}

方法二:

package lixi;


import java.util.Scanner;


public class yueshu {

public static void main(String[] args) {
System.out.println("请输入第一个数字:");
Scanner in = new Scanner(System.in);
int a = in.nextInt();
System.out.println("请输入第二个数字:");
int b= in.nextInt();

int max = yue(a,b);
System.out.println("最大公约数为:"+max);
int min = a*b/max;
System.out.println("最小公倍数为:"+min);


}


private static int yue(int a, int b) {
if(a%b==0){
return  b;
}else{
return yue(b,a%b);
}

}
}

你可能感兴趣的:(JAVA学习)