導航:首頁 > 編程語言 > java編程題

java編程題

發布時間:2022-01-24 21:50:32

『壹』 java編程

先把題目列成公式就是2*a+3*b+4*c+5*d=20

這就是一個四元一次方程
簡單點就四次循環就可以得到答案
public static void main(String[] args) {

int min=0;//不可能買負數
for (int a =min; a <=20/2; a++) {
for (int b =min; b <20/3; b++) {
for (int c = min; c <20/4; c++) {
for (int d = min; d <20/5; d++) {
if(2*a+3*b+4*c+5*d==20){
System.out.println("菊花"+a);
System.out.println("康乃馨"+b);
System.out.println("玫瑰"+c);
System.out.println("百合"+d);
}
}

}
}
}

}

『貳』 java編程題

只需要創建兩個文件就好了,第一個Sex枚舉,第二個,錄入student類。
代碼如下:

/**
* @author brave
*
* @2020-2-23 下午1:41:31
*/
public enum Sex {
MAN("男",0),WOMEN("女",1),OTHER("其他",2);

private String name;
private int index;
Sex(String name,int index){
this.name=name;
this.index=index;
}

public static String getName(int index){
for (Sex sex:Sex.values()) {
if(sex.getIndex() == index){
return sex.name;
}
}
return "";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}

}
主要定義性別有幾種類型。
以下是實現類:
/**
*
*/
package com.student;

import java.util.Scanner;

/**
* @author Brave
*
* 2020-2-23 下午3:58:07
*/
public class StudentTwo {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

String name[] = new String [3];
String number[] = new String[3];
String sex[] = new String[3];
for (int i = 0; i < 3; i++) {
int num = i+1;
System.out.println("請輸入第" + num + "個學生的學號:");
number[i] = input.next();
System.out.println("請輸入第"+ num + "個學生的姓名:");
name[i] = input.next();
System.out.println("請輸入第" + num + "個學生的性別(男請輸入0,女請輸入1,其他請輸入2):");
sex[i] = input.next();
}

System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
System.out.println("-----------------以下是錄入的學生信息----------------");

for (int i = 0; i < name.length; i++) {
System.out.println("第一個學生的姓名是:"+name[i] +",學號:"+number[i]+",性別:"+Sex.getName(Integer.parseInt(sex[i])));
}
}
}

『叄』 java題目編程題

首先初始化一個HashMap,存入一些用戶名和電話號碼的初始值,然後分別使用map的API來進行相應的map增刪改查操作就行了。

增/改:put(K key,V value),這里可以初始化用戶名為key,電話號碼為value,前提是用戶名要沒有重復的,如果是相同的用戶名會覆蓋前一個,可以當做改功能來使用
刪:remove(Object key)根據鍵,刪值,如果之前的key存的用戶名,那麼這里就是根據用戶名刪值
查:get(Object key)根據鍵查值,相當於根據用戶名查電話號碼

『肆』 java編程十道題

1.
public class TestRandomArray
{
public static void main(String[] args)
{
int maxline = (int) (Math.random()*100);
int[] array = new int[maxline];
for(int i=0;i<maxline;i++)
{
int temp = (int) (Math.random()*100);
if(temp>=10&&temp<=99)
{
array[i] = temp;
}
else
i--;
}
for(int j=0;j<maxline;j++)
{
int max = array[0];

if(array[j]>max)
{
max=array[j];
array[j] = array[0];
array[0] = max;

}
}
for(int k=0;k<maxline;k++)
{
System.out.print(array[k]+" ");
}
}
}

2.
public class TestNumber100
{
public static void main(String[] args)
{
for(int i=1;i<100;i++)
{
if(i%3==0&&(i%5!=0)&&(i%9!=0))
System.out.println(i+" ");
else
continue;
}
}
}

3.貌似沒結果
public class Test3
{
public static void main(String[] args)
{
/*for(int i=0;i<=9;i++)
{
for(int j=0;j<=9;j++)
{
int code = 10000+i*1000+j*100+23;
if((code%57)==0&&(code%67)==0&&i!=j)
{
System.out.println("x="+i+","+"y="+j);
}
else
System.out.println(i+" "+j);
}
}*/

for(int i=10023;i<=19923;i++)
if(i==0&&i==0)
System.out.println(i+" ");
}
}

4.
class Caculate {
int money;
Caculate(){
money = 10;
}
public int charge (int distance){
if (distance < 3){
money = 10;
}
if ((distance >= 3) && distance < 15)
{
money = money + 2*(distance-3);
}
if (distance > 15 )
{
money = money + 2*12 + 3*(distance - 15);
}

return money;
}
public static void main(String args[]) {

System.out.println("請輸入公里數");
int distance_example=0;
Scanner key = new Scanner(System.in);
int n = key.nextInt();
distance_example = n;
Caculate pay = new Caculate();
pay.money = pay.charge(distance_example);
System.out.println("You need to pay money:"+pay.money+" distance is:"+distance_example);

}
}

5.
public class Test4
{

public static void main (String args[])
{
int n=100;
while(n<=1000)
{
int i,j,k;
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i, 3)+Math.pow(j, 3)+Math.pow(k, 3))==n)

System.out.print(n+String.valueOf('\t'));
n++;
}
}
}

6.
public class TestMoney
{
public static void main(String[] args)
{
int one=1,two=2,five=5;
for(int i=1;i<100;i++)
for(int j=1;j<50;j++)
for(int k=1;k<20;k++)
{
if((i*one+j*two+k*five)==100)
System.out.println(i+"個一分,"+j+"個兩分,"+k+"個五分");
}

}

}

7.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestJieCheng
{
public static void main (String args[])
{
int a=0,b=1;float sum=0;
System.out.print("請輸入要計算的階乘:");
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
a=Integer.parseInt(in.readLine());
}catch(IOException e){}
for(int i=1;i<=a;i++)
{
b=b*i;

}
System.out.print(a+"的階乘是"+b);
}
}

8.
public class Test4
{

public static void main (String args[])
{
int n=100;
while(n<=1000)
{
int i,j,k;
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i, 3)+Math.pow(j, 3)+Math.pow(k, 3))==n)

System.out.print(n+String.valueOf('\t'));
n++;
}
}
}

9.
public class TestNumber2
{
public static void main(String[] args)
{
final int maxline = 20;
double sum = 0;
double [] denominator = new double [maxline];
double [] numerator = new double [maxline];
for(int i=2;i<denominator.length;i++)
{
denominator[0]=1;
denominator[1]=2;
denominator[i]=denominator[i-1]+denominator[i-2];
}
for(int j=2;j<numerator.length;j++)
{
numerator[0]=2;
numerator[1]=3;
numerator[j]=numerator[j-1]+numerator[j-2];
}
for(int k=0;k<maxline;k++)
{
sum+=(numerator[k])/denominator[k];

}
System.out.println(sum);
}

}

10.
import java.io.*;

public class 根據輸入的三個數求一元二次方程
{
public static void main (String args[]) throws NumberFormatException, IOException
{
float a[]=new float [3];
for(int i=0;i<a.length;i++)
{
System.out.println("請輸入第"+(i+1)+"個數");
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
a[i]=Float.parseFloat(in.readLine());
}
float x1=0,x2=0;
x1=(float) ((((-1)*a[1])+(Math.sqrt(a[1]*a[1]-4*a[0]*a[2])))/(2*a[0]));
x2=(float) ((((-1)*a[1])-(Math.sqrt(a[1]*a[1]-4*a[0]*a[2])))/(2*a[0]));
System.out.print(a[0]+"X2"+a[1]+"X"+a[2]+"的兩個根是:");
System.out.print(x1+"和"+x2);
}
}

『伍』 java編程題

importjava.util.Arrays;
importjava.util.Scanner;
publicclassTestExample{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
intn=Integer.parseInt(scanner.nextLine());
int[][]index=newint[n][2];//數組存儲相關信息
for(inti=0;i<n;i++){
String[]a=scanner.nextLine().split("\s");
intx=Integer.parseInt(scanner.nextLine());
index[i][0]=x;
index[i][1]=-1;
for(intj=0;j<6;j++){
if(String.valueOf(x).equals(a[j])){
index[i][1]=j;
break;
}
}
}
//數組信息輸出
for(inti=0;i<n;i++){
if(index[i][1]!=-1){
System.out.println(index[i][0]+":"+"a["+index[i][1]+"]");
}else
System.out.println(index[i][0]+":"+"notfound");
}
scanner.close();
}
}

『陸』 JAVA編程題

publicclassTest{
publicstaticvoidmain(String[]args){
int[]arr={12,22};//指定數組
intresutl=getSum(arr);//求十位數為奇數的數值的和
System.out.println(resutl);
}

publicstaticintgetSum(int[]arr){
intresult=0;//結果
for(inti=0;i<arr.length;i++){//注意這里的循環次數是arr的長度,而不是100
intnum=arr[i];//從數組里取出這個數字
if(num>9&&num<100){//題目要求數字是1~100之間的數,且有十位,那麼說明數值范圍是[10,99]
if((num/10)%2!=0){//如果十位是奇數.
result+=num;//那麼累加到結果里
}
}
}
returnresult;//返回結果
}
}

『柒』 8道簡單的Java編程題

第一題
private static void tuzinum(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
//總數
int allnum = 1;
//成年數
int chengniannum = 0;
//出生數
int chushengnum = 0;
for(int i=0;i<12;i++){
for(int j=0;j<list.size();j++){
Integer age = list.get(j);
age++;
list.set(j, age);
if(age>3){
chengniannum++;
chushengnum++;
allnum++;
list.add(1);
}
}
System.out.println("第"+i+"個月兔子對數為:"+list.size());
}
}

第二題
private static void fenjie(){
int num = 90;

boolean canchu = true;
while(canchu){
int time = 0;
for(int i=2;i<num/2;i++){
if(num%i==0){
num = num/i;
System.out.println(i);

time++;
}
}
if(time == 0 ){
canchu = false;
System.out.println(num);
}
}
}

『捌』 Java編程題

package joshua.fraction.test;
public class Fraction extends Object {
private int numerator;
private int denominator;
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public String addition(Fraction fraction){
int numerator;
int denominator;
int greatestCommonDivisor;
StringBuilder result = new StringBuilder();
result.append("(");
if(this.denominator==fraction.getDenominator()){
numerator = this.numerator + fraction.getNumerator();
denominator = this.denominator;
}else{
numerator = this.numerator*fraction.getDenominator()+fraction.getNumerator()*this.denominator;
denominator = this.denominator*fraction.getDenominator();
}
greatestCommonDivisor = getGreatestCommonDivisor(numerator, denominator);
return result.append(numerator/greatestCommonDivisor).append(")/(").append(denominator/greatestCommonDivisor).append(")").toString();
}
public String subtraction(Fraction fraction){
int numerator;
int denominator;
int greatestCommonDivisor;
StringBuilder result = new StringBuilder();
result.append("(");
if(this.denominator==fraction.getDenominator()){
numerator = this.numerator - fraction.getNumerator();
denominator = this.denominator;
}else{
numerator = this.numerator*fraction.getDenominator()-fraction.getNumerator()*this.denominator;
denominator = this.denominator*fraction.getDenominator();
}
greatestCommonDivisor = getGreatestCommonDivisor(numerator, denominator);
return result.append(numerator/greatestCommonDivisor).append(")/(").append(denominator/greatestCommonDivisor).append(")").toString();
}
@Override
public String toString() {
return "(" + numerator + ")/( " + denominator + ")";
}
@SuppressWarnings("unused")
private int getLeastCommonMultiple(int a, int b){
int sum = a*b;
return sum/getGreatestCommonDivisor(a, b);
}
private int getGreatestCommonDivisor(int a, int b){
int r;
for (;;) {
r = a % b;
a = b;
b = r;
if (r == 0) {
break;
}
}
return a;
}
}

package joshua.fraction.test;
public class Test {
public static void main(String[] args) {
Fraction a = new Fraction(1, 3);
Fraction b = new Fraction(1, 2);
System.out.println("" + a + "+" + b + "=" + a.addition(b));
System.out.println("" + a + "-" + b + "=" + a.subtraction(b));
}
}

第二題:

package joshua.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class test {
public static void main(String[] arg) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please input a Integer:");
StringBuffer sb = new StringBuffer();
Iterator<Integer> it = null;
try {
String input = reader.readLine();
it = find(Integer.valueOf((input == null || input.length() == 0) ? "0" : input)).iterator();
while (it != null && it.hasNext()) {
sb.append(it.next()).append(",");
}
if(sb.toString().length()>0)
System.out.println("result is: "+ sb.toString().substring(0, sb.toString().length() - 1));
reader.readLine();
} catch (Exception e) {
e.printStackTrace();
}
}
public static List<Integer> find(int arg){
List<Integer> list = new ArrayList<Integer>();
for(int i=1; i<arg; i++){
if(i%7==0){
list.add(i);
}else{
String str = String.valueOf(i);
char[] cs = str.toCharArray();
for(int j=0; j<cs.length; j++){
if("7".equals(String.valueOf(cs[j]))){
list.add(i);
break;
}
}
}
}
return list;
}
}

『玖』 Java-編程題

public class Main {

public static int reverseInt(int input) {
int reversedNum = 0;
while (input != 0) {
reversedNum = reversedNum * 10 + input % 10;
input = input / 10;
}
return reversedNum;
}

public static void main(String[] args) {
int input = 352;
int reversed = reverseInt(input);
System.out.println("Reversed=" + String.valueOf(reversed));
}
}

閱讀全文

與java編程題相關的資料

熱點內容
安徽蚌埠社保卡號用哪個app 瀏覽:217
php手冊下載哪個好 瀏覽:163
安卓轉蘋果為什麼轉不成功 瀏覽:226
華為榮耀系統編譯 瀏覽:733
看板塊app哪個好用 瀏覽:666
java即時編譯結果怎麼保存 瀏覽:911
java工程師在深圳 瀏覽:658
手機sql編譯軟體 瀏覽:524
外網伺服器地址購買 瀏覽:994
空調壓縮機電容價格 瀏覽:381
小程序選什麼雲伺服器 瀏覽:656
如何把java編譯回中文 瀏覽:777
天聯軟體伺服器地址是什麼 瀏覽:964
stc單片機加密 瀏覽:140
小程序地產廣告源碼 瀏覽:542
消費者信息加密私域 瀏覽:431
程序員開發團隊可以怎麼創業 瀏覽:925
設備共享伺服器是什麼意思 瀏覽:126
java符號類型 瀏覽:331
redis客戶端java 瀏覽:214