前言:
此刻姐妹们对“java计算kdj”大体比较看重,我们都需要学习一些“java计算kdj”的相关文章。那么小编在网上网罗了一些对于“java计算kdj””的相关知识,希望姐妹们能喜欢,姐妹们快快来学习一下吧!在本文中,我们将:
了解受访者就以下主题提出的基本面试问题,我们还将解决每个主题的问题
1. 将string [] 转换为字符串
//converting String[] to string
String[] actual = {"q", "s", "q"};
String converted = String.join(",", actual);
System.out.println(converted);
2. 将 int[] 转换为 int
//converting int[] to int
int[] number = {1, 2, 3};
int num = 0;
for (int i = 0; i < number.length; i++) {
num = (10 * num) + number[i];
}
System.out.println(num);
3. 在字符串中查找重复项或在字符串中出现给定字符。
//finding duplicates in String or occurrence of a given character in a String.
String text = "Sakkettyyzz";
char[] ch = text.toCharArray();
int count = 0;
for (int i = 0; i < ch.length; i++) {
for (int j = i + 1; j < ch.length; j++) {
if (ch[i] == ch[j]) {
System.out.println(ch[j]);
count++;
break;
}
}
}
System.out.println(count);
4. 在字符串中间插入字符
// insert char in middle of String
String text1 = "abcde";
StringBuffer sb = new StringBuffer(text1);
StringBuffer sb1 = sb.insert(3, 'f');
System.out.println(sb1);
5. 替换中间的字符
//replace a char in middle
String text2 = "abcde";
StringBuffer sb2 = new StringBuffer(text2);
sb2.setCharAt(2, 'F');
System.out.println(sb2);
6. 数组中的升序
int[] arr = {2, 3, 1, 5, 8};
int temp = 0; // temp,i,j
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
//assending order
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
7. 给定数字是否为素数
int numb = 3;
boolean flag = false;
for (int i = 2; i <= numb / 2; i++) {
if (numb % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(numb + " is a prime number.");
else
System.out.println(numb + " is not a prime number.");
8. 打印质数
for(int j=2;j<=10;j++)
{
int countttt=0;
for(int i=1;i<=j;i++)
{
if(j%i==0)
{
countttt++;
}
}
if(countttt==2)
System.out.print(j+" ");
}
9. 是不是字谜
String str1 = "Keep";
String str2 = "peek";
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if (str1.length() != str2.length()) {
System.out.println("not anagram");
} else {
char[] ch1 = str1.toCharArray();
char[] ch2 = str2.toCharArray();
Arrays.sort(ch1);
System.out.println(ch1);
Arrays.sort(ch2);
System.out.println(ch2);
if (Arrays.equals(ch1, ch2) == true) {
System.out.println("anagram");
} else
System.out.println("not anagram");
}
10. 删除string [] 中的重复项
//remove duplicates in string[]
String text3 = "abbacus";
Set set = new LinkedHashSet();
for (int i = 0; i < text3.length(); i++) {
set.add(text3.charAt(i));
}
System.out.println(set);
//remove duplicates in string[]
String name = "Madan";
StringBuffer sbr = new StringBuffer(name);
for (int i = 0; i < name.length(); i++) {
for (int j = i + 1; j < name.length(); j++) {
if (name.charAt(i) == name.charAt(j)) {
sbr.deleteCharAt(j);
}
}
}
System.out.println(sbr);
11. 删除 int [] 中的重复项
//remove duplicates in int[]
int[] array = {1, 3, 1, 2, 3, 5};
Set sett = new LinkedHashSet();
for (int i = 0; i < array.length; i++) {
set.add(array[i]);
}
System.out.println(sett);
12. 反向 int & string
//reverse int & string
String str = "abcde";
StringBuffer sbb = new StringBuffer(str);
System.out.println(sbb.reverse().toString());
System.out.println();
//reverse int & string
for (int i = str.length() - 1; i >= 0; i--) {
System.out.print(str.charAt(i));
}
13. 反转数字
//reverse a number
int n = 3244789;
int rev = 0;
while (n > 0) {
int rem = n % 10;
rev = (10 * rev) + rem;
n = n / 10;
}
System.out.println(rev);
14. 数字总和
//sum of numbers
int sum = 0;
int[] ar = new int[5];
Scanner in = new Scanner(System.in);
System.out.println("enter input : ");
for (int i = 0; i < ar.length; i++) {
ar[i] = in.nextInt();
sum += ar[i];
}
System.out.println(sum);
15. 交换编号
//swap number
int tempp, a = 2, b = 1;
tempp = a;
a = b;
b = tempp;
System.out.println(a);
System.out.println(b);
16. 阵列频率
//frequency of array
int[] ary = new int[5];
Scanner inn = new Scanner(System.in);
System.out.println("enter number");
for (int i = 0; i < arr.length; i++) {
ary[i] = inn.nextInt();
}
//frequency count
int[] fr = new int[ary.length];
int visited = -1;
for (int i = 0; i < ary.length; i++) {
int countt = 1;
for (int j = i + 1; j < ary.length; j++) {
if (ary[i] == ary[j]) {
countt++;
fr[j] = visited;
}
}
if (fr[i] != visited) {
fr[i] = countt;
}
}
for (int i = 0; i < fr.length; i++) {
if (fr[i] != visited)
System.out.println(ary[i] + " count is : " + fr[i]);
}
17. 数组中的 Shift 元素
//shift element in array
int[] aarr = {1, 2, 3, 4, 5};
for (int i = 0; i < aarr.length; i++) {
System.out.print(aarr[i]);
}
int nn = 1;
for (int i = 0; i < nn; i++) {
int first = aarr[0];
int j;
for (j = 0; j < aarr.length - 1; j++) {
aarr[j] = aarr[j + 1];
}
aarr[j] = first;
}
System.out.println();
for (int i = 0; i < aarr.length; i++) {
System.out.print(aarr[i]);
}
18. 在数组中打印偶数索引/奇数索引
//print even index in array / odd index
int[] aaray = {1, 2, 3, 4, 5};
for (int i = 1; i < aaray.length; i = i + 2) {
System.out.println(aaray[i]);
}
for (int i = 0; i < aaray.length; i = i + 2) {
System.out.println(aaray[i]);
}
19. 在阵列中拆分
//split in array
String split = "hello hi when";
StringTokenizer st = new StringTokenizer(split);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
20. 计算拆分数组中的总字数
//count total word in split array
String cnt = "hello hi when";
StringTokenizer stt = new StringTokenizer(cnt);
int cntt = 0;
while (stt.hasMoreTokens()) {
System.out.println(stt.nextToken());
cntt++;
}
System.out.println(cntt);
21. 字符串中的总字符数
// total char in String
String totcount = "hellohiwhen";
int counttt = 0;
for (int i = 0; i < totcount.length(); i++) {
totcount.charAt(i);
counttt++;
}
System.out.println(counttt);
22.删除数组中的空格
//remove spaces in array
String spc = " helloh iwhe n ";
spc = spc.trim();
spc = spc.replaceAll("\\s", "");
System.out.println(spc);
23. 使用subString,StringTokenizer,WordUtils.capitalize
//StringTokenizer
String sentence = "techie delight is awesome!";
StringTokenizer stok = new StringTokenizer(sentence);
int coun = 0;
while (st.hasMoreTokens()) {
for (int i = 0; i < sentence.length(); i++) {
if (i >= 0 && count != 4) {
String txt = st.nextToken();
count++;
String updatedText = txt.substring(0, 1).toUpperCase() + txt.substring(1);
System.out.println(updatedText);
System.out.println(count);
} else if (count == count) {
System.out.println("-----------------");
break;
}
}
}
// ----------------------------------------
//subString
class Abacus {
public String capitalizeWord(String str) {
String words[] = str.split("\\s");
String concadinating = "";
for (String w : words) {
String fn = w.substring(0, 1).toUpperCase(Locale.ROOT);
String second = w.substring(1);
concadinating += fn + second + " ";
}
return concadinating.trim();
}
}
class TestStringFormatter {
public void main(String[] args) {
//System.out.println(tests.Abacus.capitalizeWord("my name is khan"));
//System.out.println(tests.Abacus.capitalizeWord("I am sonoo jaiswal"));
}
}
//---------------------------
//WordUtils.capitalize
String strings = "hello i am";
// strings = WordUtils.capitalize(strings);
System.out.println(strings);
24.编写一个java程序来反转字符串中的每个单词?
//Write a java program to reverse each word in string?
String strr = "hello i am"; // olleh i ma
StringTokenizer sty = new StringTokenizer(strr);
String concadinate = "";
while (sty.hasMoreTokens()) {
String word = sty.nextToken();
StringBuffer s = new StringBuffer(word);
concadinate += s.reverse().toString() + " ";
}
System.out.println(concadinate);
25. 在字符串中替换、插入、删除
//Replace , insert , delected in String
String strii = "kdjb";
StringBuffer sbn = new StringBuffer(str);
str = sb.deleteCharAt(0).toString();
System.out.println(str);
str = sb.insert(0, "A").toString();
System.out.println(str);
sb.setCharAt(2, 'L');
System.out.println(sb);
26.编写一个java程序来切换字符串中的每个单词?
public static String charChanger(String str) {
String cone = "";
String[] word = str.split("\\s");
for (String w : word) {
String fn = w.substring(0, 1).toLowerCase(Locale.ROOT);
String ln = w.substring(1).toUpperCase(Locale.ROOT);
cone += fn + ln + " ";
}
return cone.trim();
}
}
class Solution {
public static void main(String[] args) {
System.out.println(Abacus.charChanger("hello am i"));
}
}
27. 给定一个数字,打印其数字的总和,直到它是个位数
//Input: 563
int[] num ={5,6,3};
int sum=0;
String total;
int addition = 0;
for (int i=0;i<num.length;i++){
sum +=num[i];
}
System.out.println(sum);
if (sum!=0){
total=String.valueOf(sum);
if (total.length() >1){
for (int i=0;i<total.length();i++){
addition +=Integer.parseInt(String.valueOf(total.charAt(i))) ;
}
}
}
System.out.println(addition);
28. 给定一个数字,递增数字的奇数数字并递减数字的偶数位。
//Input - 12309
//Output - 21490
int[] num = {1, 2, 3, 0, 9};
int even = 0;
int odd = 0;
for (int i = 0; i < num.length; i++) {
if (num[i] % 2 == 0 && num[i] != 9 && num[i] != 0) {
even = num[i] - 1;
System.out.println(even);
} else if (num[i] % 2 != 0 && num[i] != 9 && num[i] != 0) {
odd = num[i] + 1;
System.out.println(odd);
} else {
if (num[i] == 0)
System.out.println(num[4]);
if (num[i] == 9)
System.out.println(num[3]);
}
}
29.在给定字符串中,取出字符串的中间字符并形成单独的字符串
// Input : How are you friend
// Output : oroe
String str1 = "How are you friend";
String[] str= str1.split("\\s");
for (int i=0;i<str.length;i++){
int size=str[i].length();
int half=size/2;
if (size == 3){
char ch=str[i].charAt(1);
System.out.print(ch);
}
if (size/2 == 3){
char ch=str[i].charAt(half);
System.out.print(ch);
}
30. 给定一个数字,递增数字的奇数数字并递减数字的偶数位。
//Input - 12309
//Output - 21490
int[] num = {1, 2, 3, 0, 9};
int even = 0;
int odd = 0;
for (int i = 0; i < num.length; i++) {
if (num[i] % 2 == 0 && num[i] != 9 && num[i] != 0) {
even = num[i] - 1;
System.out.println(even);
} else if (num[i] % 2 != 0 && num[i] != 9 && num[i] != 0) {
odd = num[i] + 1;
System.out.println(odd);
} else {
if (num[i] == 0)
System.out.println(num[4]);
if (num[i] == 9)
System.out.println(num[3]);
}
31. 编写 Java 程序来找出数组中的前两个最大值?
int[] list = { 95, 24, 48, 11, 43, 13, 79, 80 }; // temp,i,j
int temp=0;
for (int i=0;i<list.length;i++){
System.out.print(list[i]+",");
}
for (int i=0;i<list.length;i++){
for (int j=i+1;j<list.length;j++){
if (list[i] > list[j]){
temp = list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
System.out.println();
for (int i=0;i<list.length;i++){
System.out.print(list[i]+",");
}
int max1=list.length-1;
int max2=list.length-2;
System.out.println();
System.out.println(list[max1]);
System.out.println(list[max2]);
希望以上主题能帮助初学者进行一些动手练习,为面试做准备。
标签: #java计算kdj