Java Main()中的While和if循环希望我删除它下面的代码?
在下面的代码中创建While和IF循环时,会产生一个错误,提示:Java Main()中的While和if循环希望我删除它下面的代码?,java,for-loop,while-loop,Java,For Loop,While Loop,在下面的代码中创建While和IF循环时,会产生一个错误,提示:Unreachable code,并建议我删除下面的代码。我不理解这里的问题,我尝试将while语句和Main()方法中其余代码的下面的代码一起移动,但是我的JFrame只是显示为白色。有什么帮助吗 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.
Unreachable code
,并建议我删除下面的代码。我不理解这里的问题,我尝试将while语句和Main()方法中其余代码的下面的代码一起移动,但是我的JFrame只是显示为白色。有什么帮助吗
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.security.auth.x500.X500Principal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.omg.CORBA.PRIVATE_MEMBER;
public class Game
{
boolean p1;
private int counterY1 = 515, counterY2 = 515, counterY3 = 515, counterY4 = 515;
boolean playerTurn = true;
boolean playerTurn2 = false;
boolean moveLoop = true;
public void moveC1Up()
{
counterY1 -= 51 * diceRoll();
}
public void moveC2Up()
{
counterY2 -= 51 * diceRoll();
}
public void moveC3Up()
{
counterY3 -= 51 * diceRoll();
}
public void moveC4Up()
{
counterY4 -= 51 * diceRoll();
}
public int diceRoll()
{
int randGen = (int)(Math.random()*1) + 1;
System.out.print(randGen);
return randGen;
}
private JButton moveC1But, moveC2But, rollDiceButton;
private JLabel amountRolledLabel;
public Game()
{
JFrame window = new JFrame ("Main Game");
final JPanel firstPanel = new JPanel(new GridLayout(3,1))
{
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
int width = getWidth() / 3;
int height = getHeight() / 11;
for (int i = 0; i < 4; i++) {
g.drawLine(i * width, 0, i * width, getHeight());
}
for (int i = 0; i < 11; i++) {
g.drawLine(0, i * height, getWidth(), i * height);
}
g.setColor(Color.DARK_GRAY);
g.drawOval(220, counterY1, 40, 40);
g.fillOval(220, counterY1, 40, 40);
g.drawOval(300, counterY2, 40, 40);
g.fillOval(300, counterY2, 40, 40);
g.setColor(Color.LIGHT_GRAY);
g.drawOval(410, counterY3, 40, 40);
g.fillOval(410, counterY3, 40, 40);
g.drawOval(490, counterY4, 40, 40);
g.fillOval(490, counterY4, 40, 40);
}
};
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(firstPanel, BorderLayout.CENTER);
JPanel rightSidePanel = new JPanel(new GridLayout(10,1));
moveC1But = new JButton("Move Counter 1");
moveC2But = new JButton("Move Counter 2");
rollDiceButton = new JButton("Roll Dice");
rightSidePanel.add(moveC1But, BorderLayout.LINE_START);
rightSidePanel.add(moveC2But, BorderLayout.LINE_END);
rightSidePanel.add(rollDiceButton, BorderLayout.SOUTH);
mainPanel.add(rightSidePanel, BorderLayout.EAST);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(mainPanel);
window.setSize(700, 600);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setResizable(false);
while(moveLoop == true)
{
moveC1But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
diceRoll();
moveC1Up();
firstPanel.repaint();
System.out.print(diceRoll());
System.out.print("Test1");
playerTurn = false;
playerTurn2 = true;
moveLoop = false;
}
});
moveC2But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
diceRoll();
moveC3Up();
firstPanel.repaint();
System.out.print(diceRoll());
System.out.print("Test1");
playerTurn = false;
playerTurn2 = true;
moveLoop = false;
}
});
}
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Game();
}
});
}
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Random;
导入javax.security.auth.x500.x500主体;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入org.omg.CORBA.PRIVATE_成员;
公开课游戏
{
布尔p1;
私有int counterY1=515,counterY2=515,counterY3=515,counterY4=515;
布尔值playerTurn=true;
布尔playerTurn2=false;
布尔moveLoop=true;
公共void moveC1Up()
{
计数器Y1-=51*diceRoll();
}
公共无效向上移动()
{
计数器y2-=51*diceRoll();
}
公共无效向上移动()
{
计数器3-=51*diceRoll();
}
公共无效向上移动()
{
计数器4-=51*diceRoll();
}
公共卷
{
int randGen=(int)(Math.random()*1)+1;
系统输出打印(randGen);
返回兰登;
}
私有JButton moveC1But、moveC2But、rolldice按钮;
私人JLabel amontrolled标签;
公共游戏()
{
JFrame窗口=新JFrame(“主游戏”);
最终JPanel firstPanel=新JPanel(新网格布局(3,1))
{
公共组件(图形g)
{
Graphics2D g2d=(Graphics2D)g;
超级油漆组件(g2d);
int width=getWidth()/3;
int height=getHeight()/11;
对于(int i=0;i<4;i++){
g、 抽绳(i*width,0,i*width,getHeight());
}
对于(int i=0;i<11;i++){
g、 抽绳(0,i*高度,getWidth(),i*高度);
}
g、 setColor(颜色:深灰色);
g、 drawOval(220,计数器1,40,40);
g、 椭圆形(220,1,40,40);
g、 drawOval(300,计数器2,40,40);
g、 圆形(300,反2,40,40);
g、 setColor(颜色:浅灰色);
g、 drawOval(410,反3,40,40);
g、 圆角(410,反3,40,40);
g、 drawOval(490,计数器4,40,40);
g、 圆角(490,反4,40,40);
}
};
JPanel mainPanel=newjpanel(newborderlayout());
添加(第一个面板,BorderLayout.CENTER);
JPanel rightSidePanel=新JPanel(新网格布局(10,1));
moveC1But=新的JButton(“移动计数器1”);
moveC2But=新的JButton(“移动计数器2”);
rollDiceButton=新的JButton(“滚动骰子”);
添加(moveC1But,BorderLayout.LINE_START);
右侧面板。添加(moveC2But,BorderLayout.LINE_END);
添加(rollDiceButton,BorderLayout.SOUTH);
主面板。添加(右侧面板,边框布局。东);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(主面板);
设置窗口大小(700600);
window.setLocationRelativeTo(空);
window.setVisible(true);
window.setresizeable(false);
while(moveLoop==true)
{
moveC1But.addActionListener(新ActionListener(){
已执行的公共无效行动(行动事件ae)
{
骰子卷();
moveC1Up();
firstPanel.repaint();
System.out.print(diceRoll());
系统输出打印(“测试1”);
playerTurn=false;
playerTurn2=真;
moveLoop=false;
}
});
moveC2But.addActionListener(新ActionListener(){
已执行的公共无效行动(行动事件ae)
{
骰子卷();
moveC3Up();
firstPanel.repaint();
System.out.print(diceRoll());
系统输出打印(“测试1”);
playerTurn=false;
playerTurn2=真;
moveLoop=false;
}
});
}
}
公共静态void main(字符串参数[])
{
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
新游戏();
}
});
}
}
因为没有break
语句来中断无限循环
这就是为什么compile会显示错误无法访问的代码
,因为下面的代码永远无法访问(true)
。因为没有break
语句来中断无限循环
这就是为什么compile会向您显示错误无法访问的代码
,因为下面的代码while(true)
永远无法访问。while(true)
将永远继续,因为您在任何地方都没有break
语句可以离开循环。这就意味着循环下面的所有代码永远无法执行 while(true)
将永远持续,因为您在任何地方都没有break
语句可以离开循环。这就意味着循环下面的所有代码永远无法执行 在当前代码中,循环不允许JFrame调用repaint()因为它在用户单击按钮之前是不会被破坏的,wh
package beaudoin.apps;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
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.SwingUtilities;
import sun.awt.RepaintArea;
public class Game {
boolean p1;
private int counterY1 = 515, counterY2 = 515, counterY3 = 515, counterY4 = 515;
boolean playerTurn = true;
boolean playerTurn2 = false;
public void moveC1Up() {
counterY1 -= 51 * diceRoll();
}
public void moveC2Up() {
counterY2 -= 51 * diceRoll();
}
public void moveC3Up() {
counterY3 -= 51 * diceRoll();
}
public void moveC4Up() {
counterY4 -= 51 * diceRoll();
}
public int diceRoll() {
int randGen = (int) (Math.random() * 1) + 1;
System.out.print(randGen);
return randGen;
}
private JButton moveC1But, moveC2But, rollDiceButton;
private JLabel amountRolledLabel;
public Game() {
JFrame window = new JFrame("Main Game");
final JPanel firstPanel = new JPanel(new GridLayout(3, 1)) {
private static final long serialVersionUID = -1729570833533906839L;
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
int width = getWidth() / 3;
int height = getHeight() / 11;
for (int i = 0; i < 4; i++) {
g.drawLine(i * width, 0, i * width, getHeight());
}
for (int i = 0; i < 11; i++) {
g.drawLine(0, i * height, getWidth(), i * height);
}
g.setColor(Color.DARK_GRAY);
g.drawOval(220, counterY1, 40, 40);
g.fillOval(220, counterY1, 40, 40);
g.drawOval(300, counterY2, 40, 40);
g.fillOval(300, counterY2, 40, 40);
g.setColor(Color.LIGHT_GRAY);
g.drawOval(410, counterY3, 40, 40);
g.fillOval(410, counterY3, 40, 40);
g.drawOval(490, counterY4, 40, 40);
g.fillOval(490, counterY4, 40, 40);
}
};
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(firstPanel, BorderLayout.CENTER);
JPanel rightSidePanel = new JPanel(new GridLayout(10, 1));
moveC1But = new JButton("Move Counter 1");
moveC1But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
diceRoll();
moveC1Up();
firstPanel.repaint();
playerTurn = false;
playerTurn2 = true;
}
});
moveC2But = new JButton("Move Counter 2");
moveC2But.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
diceRoll();
moveC3Up();
firstPanel.repaint();
playerTurn = false;
playerTurn2 = true;
}
});
rollDiceButton = new JButton("Roll Dice");
rightSidePanel.add(moveC1But, BorderLayout.LINE_START);
rightSidePanel.add(moveC2But, BorderLayout.LINE_END);
rightSidePanel.add(rollDiceButton, BorderLayout.SOUTH);
mainPanel.add(rightSidePanel, BorderLayout.EAST);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(mainPanel);
window.setSize(700, 600);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setResizable(false);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Game();
}
});
}
}