題目一:
參考代碼
importjava.util.Scanner;
publicclassJiaFaDemo{
publicstaticvoidmain(String[]args){
intx=(int)(Math.random()*100);
inty=(int)(Math.random()*100);
System.out.println(x+"+"+y+"="+(x+y));//自動答題58+4=62
//System.out.print(x+"+"+y+"=");//人工輸入答題需要導入包importjava.util.Scanner;
//Scannerinput=newScanner(System.in);
//intz=input.nextInt();
//if(z==(x+y)){
//System.out.println("回答正確");
//}else{
//System.out.println("回答錯誤");
//}
}
}
輸出
58+4=62
題目二
publicclassJiShuDemo{
publicstaticvoidmain(String[]args){
intsum=0;
for(inti=1;i<=50;i++){
if(i%2!=0){//不能被2整除的數是奇數
sum+=i;
}
}
System.out.println("1~50間奇數和="+sum);
}
}
輸出
1~50間奇數和=625
❷ 這道java編程題怎麼做
import java.util.*;
class Test1{
public LinkedList res=new LinkedList();
public Scanner item=new Scanner(System.in);
}
class Test2 extends Test1{
private void calc(){
System.out.println("輸入4個字元");
for(int x=0;x<4;x++){
char tem=item.next().charAt(0);
if(Character.isUpperCase(tem)){
tem=Character.toLowerCase(tem);
res.add(tem);
}else if(Character.isLowerCase(tem)){
tem=Character.toUpperCase(tem);
res.add(tem);
}else if(Character.isDigit(tem)){
res.add(tem);
}else{
String str="other";
res.add(str);
}
}
for(Object y:res){
System.out.println(y);
}
}
public static void main(String[] args){
new Test2().calc();
}
}
❸ JAVA編程基礎題目,怎麼寫,求大神詳細指導,謝謝!
//代碼如下
import java.until.Scanner;
Class Test(){
float dc;//表示折扣
public static void main(String []arg0)
{
Scanner sca=new Scanner(System.in);
System.out.println(「請輸入是否為會員:是(y)/否(其他字元)」);
String a=sca.next();
System.out.println(「請輸入購物金額:」);
float b=sca.nextFloat();
//嵌套if代碼段
if(a.equal("y"))
{
if(b>=200) dc=0.75
else dc=8
}
else{
if(b>=100) dc=0.9
else dc=1
}
float count=b*dc;//實際支付金額
Sytem.out.println("實際支付:"+count);//最後列印
}
}
❹ java編程題,期末考試的題,跪求,求大佬編寫一下
publicclassRunTest{
privatestaticint[]arr={2,5,7,1,3,4,6,9,8};
publicstaticvoidmain(String[]args){
System.out.println("原始:"+Arrays.toString(arr));
newThread(){
publicvoidrun(){
StringBuildersd=newStringBuilder();
for(inta:arr){
sd.append(a+"");
}
System.out.println("反轉:["+sd.reverse()+"]");
}
}.start();
newThread(){
publicvoidrun(){
for(inti=0;i<arr.length-1;i++){
for(intj=0;j<arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
inttemp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("冒泡:"+Arrays.toString(arr));
}
}.start();
}
}
❺ JAVA編程題!
//把你的那個表作成test3.txt放到D盤根,跑程序就好了
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class MyTest3 {
List stuInfoList = new ArrayList();
public MyTest3(){
printResult();
}
public void readFile() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\test3.txt")));
String line = "";
int i=0;
while ((line = br.readLine()) != null) {
if(i++>0){ //略過頭上的漢字行
StudentInfo student = new StudentInfo(line.split(" "));
stuInfoList.add(student);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
class StudentInfo implements Comparable{
public int stuId;
public double pings;
public double qizhong;
public double qimo;
public double bishi;
public double zhongFeng;
public StudentInfo(){};
public StudentInfo(String[] info){
this.stuId = Integer.parseInt(info[0]);
this.pings = Integer.parseInt(info[1]);
this.qizhong = Integer.parseInt(info[2]);
this.qimo = Integer.parseInt(info[3]);
this.bishi = Integer.parseInt(info[4]);
this.zhongFeng = pings*0.1+qizhong*0.25+qimo*0.15+bishi*0.5;
}
public String getPingJunFeng(int size){
return pings/size +" "+qizhong/size+" "+qimo/size+" "+bishi/size+" "+zhongFeng/size;
}
public String toString(){
return stuId + " " +pings + " "+qizhong+" "+qimo+" "+bishi+" "+zhongFeng;
}
public int compareTo(Object arg0) {
StudentInfo info = (StudentInfo)arg0;
return (int)(info.zhongFeng-this.zhongFeng);
}
}
public void printResult(){
readFile();
System.out.println("學號 平時 期中 期末 筆試 總評分");
for(Iterator it=stuInfoList.iterator();it.hasNext();){
System.out.println(it.next());
}
System.out.println("-----------80分以上---------------\r\n學號 總評分");
for(Iterator it=stuInfoList.iterator();it.hasNext();){
StudentInfo info = (StudentInfo)it.next();
if(info.zhongFeng>=80)
System.out.println(info.stuId + " "+info.zhongFeng);
}
System.out.println("-----------沒有及格---------------\r\n學號 總評分");
for(Iterator it=stuInfoList.iterator();it.hasNext();){
StudentInfo info = (StudentInfo)it.next();
if(info.zhongFeng<60)
System.out.println(info.stuId + " "+info.zhongFeng);
}
Collections.sort(stuInfoList);
System.out.println("-----------排序之後---------------\r\n學號 平時 期中 期末 筆試 總評分");
for(Iterator it=stuInfoList.iterator();it.hasNext();){
System.out.println(it.next());
}
StudentInfo pinjunfeng = new StudentInfo();
for(Iterator it=stuInfoList.iterator();it.hasNext();){
StudentInfo info = (StudentInfo)it.next();
pinjunfeng.bishi+=info.bishi;
pinjunfeng.pings+=info.pings;
pinjunfeng.qimo+=info.qimo;
pinjunfeng.qizhong+=info.qizhong;
pinjunfeng.zhongFeng+=info.zhongFeng;
}
System.out.println("-----------平均分---------------\r\n平時 期中 期末 筆試 總評分");
System.out.println(pinjunfeng.getPingJunFeng(stuInfoList.size()));
}
public static void main(String[] args) throws Exception {
new MyTest3();
}
}
❻ 大一編程入門java語言,這道題代碼怎麼打呢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PainterPanel extends JPanel implements MouseListener{
int shape=-1; //圖案類型
Point[] point=new Point[2]; //記錄滑鼠拖動的起始點和終點
public PainterPanel(){
super(); //調用父類構造函數
this.setBackground(Color.white); //設置背景顏色
point[0]=new Point(-1,-1); //初始化變數
point[1]=new Point(-1,-1);
addMouseListener(this); //增加滑鼠事件
}
public void mouseReleased(MouseEvent e){ //滑鼠釋放事件
point[1]=new Point(e.getX(),e.getY()); //設置終點位置
repaint(); //重繪屏幕
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){ //滑鼠按下時事件
point[0]=new Point(e.getX(),e.getY()); //設置起始位置
}
public void paint(Graphics g){
super.paint(g);
switch (shape){ //根據shape繪制圖形
case 0:
g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //繪線
break;
case 1:
int width=point[1].x-point[0].x;
int height=point[1].y-point[0].y;
g.drawOval(point[0].x,point[0].y,width,height); //繪橢圓
break;
case 2:
width=point[1].x-point[0].x;
height=point[1].y-point[0].y;
g.drawRect(point[0].x,point[0].y,width,height); //繪矩形
break;
}
}
public void drawShape(int shape){
this.shape=shape;
}
}
❼ java編程題目,明天考試用,急啊!!
第一題代碼太多了,這里不方便寫了
第二題還是比較簡單,只需要針對字元串每個字元遍歷(String.length),當然題目裡面要求用while循環,否則直接用一個方法就可以了。當然還是需要統計轉換的個數。這里給你寫個主要的內容吧,其他自己寫。
int i=0;
int count=0;
string str1;
while (i<str.length)
{
if (str.indexOf(i)>=97 && str.indexOf(i)<=123)
{
str1+=str.indexOf(i)-32;//這個地方不好,但是對於這個題目沒關系
count++;
}
else
{
str1+=str.indexOf(i);
}
}
第三題也不難,會定義數組就一定會,但是這個裡面也有問題,應為JAVA從鍵盤里讀入數據,好像沒那麼簡單,不過,這個題目裡面,這部分不寫也不會有太大問題。
public class Test
{
public static void main(string[] args)
{
int count= convert(args);//轉換成數值
String[] num=new String(count);
//遍歷
for (int i=0;i<num.count;i++)
{
//這裡面完成累加就可以了。
}
}
}
我只能給你這么多了,再給你答案就是害你了。努力吧
❽ java編程題目這個怎麼弄啊。不會啊求解答
Suansu.java
public class Suansu{
private int a;
private int b;
public Suansu(){ //構造函數 初始化a=10,b=5;
this.a=10;
this.b=5;
}
public int AddAB(){ //和
return a+b;
}
public int SubAB(){ //差
return a-b;
}
}
TestSuansu.java
public class TestSuansu{
public static void main(String[] args){
Suansu ss = new Suansu();
System.out.println("a + b = "+ ss.AddAB());
System.out.println("a - b = "+ ss.SubAB());
}
}
❾ java面向對象程序設計期末考試編程題!急!!!
===============================第一題==============================
import java.applet.Applet;
import java.awt.Color;
import java.awt.Label;
public class test extends Applet {
private Label label;
@Override
public void init() {
label=new Label("歡迎來到java世界!");
label.setBackground(Color.BLUE);
setBackground(Color.PINK);
add(label);
}
}
===============================第二題==============================
因為沒圖,所以自己設計的界面..
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class test implements ActionListener {
private JFrame frame;
private JLabel label;
private JTextArea jt;
private JScrollPane jsp;
private JButton show;
public test() {
frame=new JFrame("Test");
label=new JLabel("顯示內容");
jt=new JTextArea(10,20);
jsp=new JScrollPane(jt);
show = new JButton("顯示給定內容");
JPanel panel=new JPanel();
panel.add(label);
panel.add(show);
frame.add(jsp,BorderLayout.NORTH);
frame.add(panel,BorderLayout.SOUTH);
show.addActionListener(this);
show();
}
public void show(){
frame.setLocation(200, 200);
frame.setSize(300, 260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
label.setText(jt.getText());
}
public static void main(String[] args) {
new test().show();
}
}
❿ java的編程題,要把代碼答在卷子上要怎麼寫呢
{
privateintscore;
publicvoidsetScore(intscore){
this.score=score;
}
publicintgetScore(){
returnthis,score;
}
//show方法
publicvoidshow(){
System.out.println(this.getName()+":"+score);
}
//還要實現介面的其他方法
}
下面是測試單元:
@Test
public void test(){
Student s = new Student();
s.setName("sss");
s.setScore(88);
s.show();
}