第一個回答是對的。以輸入為3,1為例:
目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、紅河網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
首次調(diào)用sum(3,1)
count=1
for (int i = 0;i count;i++) {
temp += a ;
}
i取0 temp=3
因為counta (因為a=3)
則調(diào)用sum(3,2)
count=2
for (int i = 0;i count;i++) {
temp += a ;
}
i取0, temp=3
i取1 temp=33
因為counta (因為a=3)
則調(diào)用sum(3,3)
count=3
for (int i = 0;i count;i++) {
temp += a ;
}
i取0, temp=3
i取1 temp=33
i取2 temp=333
然后求和3+33+333
這里面涉及到了字符串和整數(shù)之間的轉(zhuǎn)換。
temp += a ;即把字符串temp末尾加上a(這里面會自動把整數(shù)型的數(shù)據(jù)a轉(zhuǎn)換為字符串)
Integer.parseInt(temp) ;即將字符創(chuàng)temp轉(zhuǎn)換為整數(shù)型。
以此類推。輸入2,1
結(jié)果為2+22=24;
輸入3,1
結(jié)果為3+33+333;
輸入4,1
結(jié)果為4+44+444+4444;
輸入3,2
結(jié)果為3+33
不知道我說的是否清楚
提示輸入一個大于2且11的數(shù)字
輸入一整型數(shù)值給Vertices,
if(Vertices 3 || Vertices 11){
提示重新輸入且應(yīng)輸入
退出程序
}else{
生成一個Vertices * Vertices 大小的數(shù)組Graph,
填充數(shù)組 :行號與列號相同填充0,其余填充10以內(nèi)隨機數(shù)
交換元素:以[i][j]位置的數(shù)值與[j][i]位置的數(shù)值互換
最后打印數(shù)組各元素
}
class NoLowerLetterException extends Exception {
public NoLowerLetterException(String msg) {
super(msg);
}
}
class NoDigitException extends Exception {
public NoDigitException(String msg) {
super(msg);
}
}
class People {
void printLetter(char c) {
if (c = 'a' c = 'z') {
System.out.println(c);
} else {
try {
throw new NoLowerLetterException(String.valueOf(c));
} catch (NoLowerLetterException e) {
e.printStackTrace();
}
}
}
void printDigit(char c) {
if (c = '0' c = '9') {
System.out.println(c);
} else {
try {
throw new NoDigitException(String.valueOf(c));
} catch (NoDigitException e) {
e.printStackTrace();
}
}
}
}
public class ExceptionExample {
public static void main(String args[]) {
People people = new People();
for (int i = 0; i 128; i++) {
// 【代碼5】
// //將i轉(zhuǎn)換為char類型,執(zhí)行people.printLetter()方法,如果出現(xiàn)異常則捕獲,并輸出異常的錯誤信息!
people.printLetter((char) i);
}
for (int i = 0; i 128; i++) {
// 【代碼6】 //將i轉(zhuǎn)換為char類型,執(zhí)行people. printDigit
// ()方法,如果出現(xiàn)異常則捕獲,并輸出異常的錯誤信息!
people.printDigit((char) i);
}
}
}
代碼如下,望采納
public?class?PrintPrime{
public?static?void?main(String?args[]){
//設(shè)置一個計數(shù)變量count,用于統(tǒng)計一行當(dāng)中已經(jīng)輸出數(shù)字的個數(shù)
int?count?=?0;
//寫代碼時人為判斷200為非素數(shù),如果不考慮題目的嚴(yán)格要求的話,可以寫成200
for(int?i?=?100;i=200;i++){
//判斷數(shù)字是否為素數(shù),若是,則count+1并輸出數(shù)字
if(PrintPrime.IsPrime(i)){
count++;
System.out.print(i+"?");
}
//如果一行十個已經(jīng)輸出完畢,計數(shù)歸零,換行
if(count==10){
count=0;
System.out.println();
}
}
}
//判斷數(shù)字是否為素數(shù)
public?static?boolean?IsPrime(int?n){
//如果小于等于三,則大于一即為素數(shù)
if?(n?=?3)?{
return?n??1;
}
//從2循環(huán)到數(shù)字的開平方,算法優(yōu)化
for(int?i=2;i=Math.sqrt(n);i++){
if(n%i?==?0)
return?false;
}
return?true;
}
}
自己純手打,老半天才弄出來啊
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Demo2 extends JFrame {
private JLabel lb1, lb2, lb3, lb4; // 提示標(biāo)簽
private JTextField ta1, ta2;// 兩個文本框
private JButton b1, b2, b3; // 三個按鈕
private JPanel p1, p2; // 兩個JPanel面板
public Demo2() {
// 初始化所有組件
lb1 = new JLabel("歡迎使用人機猜拳程序");
lb2 = new JLabel("你出拳: ");
lb3 = new JLabel("電腦出拳:");
lb4 = new JLabel("結(jié)果");
ta1 = new JTextField();
ta1.setPreferredSize(new Dimension(60, 60)); // 設(shè)置大小
ta1.setEditable(false);//設(shè)置不可編輯
ta2 = new JTextField();
ta2.setPreferredSize(new Dimension(60, 60));
ta2.setEditable(false);//設(shè)置不可編輯
b1 = new JButton("剪刀");
b2 = new JButton("石頭");
b3 = new JButton("布");
p1 = new JPanel();
p2 = new JPanel();
// 設(shè)置第一個面板內(nèi)容
Box box = Box.createVerticalBox();
Box box1 = Box.createHorizontalBox();
box1.add(lb2);
box1.add(ta1);
box1.add(lb3);
box1.add(ta2);
box.add(lb1);
box.add(Box.createVerticalStrut(40));
box.add(box1);
box.add(Box.createVerticalStrut(10));
box.add(lb4);
box.add(new JLabel());
p1.add(box);
// 設(shè)置第二個面板
p2.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器
p2.setPreferredSize(new Dimension(0, 60));
GridBagConstraints g2 = new GridBagConstraints();
g2.fill = GridBagConstraints.BOTH;
g2.weightx = 1.0;
g2.weighty = 1.0;
g2.gridx = 0;
g2.gridy = 0;
p2.add(b1, g2);
g2.gridx = 1;
p2.add(b2, g2);
g2.gridx = 2;
p2.add(b3, g2);
//為3個按鈕添加事件
b1.addActionListener(new buttonAction());
b2.addActionListener(new buttonAction());
b3.addActionListener(new buttonAction());
this.getContentPane().add(p1);
this.getContentPane().add(p2, BorderLayout.SOUTH);
this.setTitle("機器人猜拳游戲");
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//事件類
class buttonAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
ta1.setText("剪刀");
init(ta1.getText());
}else if(e.getSource()==b2){
ta1.setText("石頭");
init(ta1.getText());
}else if(e.getSource()==b3){
ta1.setText("布");
init(ta1.getText());
}
}
// 模擬電腦出拳,產(chǎn)生三個隨機數(shù)。0代表剪刀,1代表石頭,2代表布
public String getQuan(){
String str="";
int num=new Random().nextInt(3) ;
if(num==0){
str="剪刀";
}else if(num==1){
str="石頭";
}else if(num==2){
str="布";
}
return str;
}
// 判斷輸贏方法
public String isying(String s1,String s2){
String s="";
if(s1.equals(s2)){
s="平局";
}else if(s1.equals("剪刀")s2.equals("布")){
s="你贏";
}else if(s1.equals("石頭")s2.equals("剪刀")){
s="你贏";
}else if(s1.equals("布")s2.equals("石頭")){
s="你贏";
}else{
s="電腦贏";
}
return s;
}
public void init(String wo){
String sy=""; // 保存輸贏結(jié)果
String dncq=getQuan(); //電腦出拳
if(wo.equals(dncq)){
sy="平局";
}else if(wo.equals("剪刀")dncq.equals("布")){
sy="你贏";
}else if(wo.equals("石頭")dncq.equals("剪刀")){
sy="你贏";
}else if(wo.equals("布")dncq.equals("石頭")){
sy="你贏";
}else{
sy="電腦贏";
}
ta2.setText(dncq);// 電腦出拳
lb4.setText("結(jié)果:"+sy);
}
}
public static void main(String[] args) {
new Demo2();
}
}