Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java行混淆_Java - Fatal编程技术网

Java行混淆

Java行混淆,java,Java,我的java代码有问题。我正试图让左上象限产生一组由用户通过JOption窗格输入的行,这些行的颜色和位置都是随机的。程序构建成功,但它不会生成用户输入的行数,也不会设置随机颜色(这在我的代码底部)。有人能解释一下如何解决这个问题吗?非常感谢。 编辑:修复了曲线大括号,但仍然不起作用。 编辑:除随机颜色外,所有颜色现在都正常工作 import javax.swing.*; //for JFrame import java.awt.*; //for Graphics and Cont

我的java代码有问题。我正试图让左上象限产生一组由用户通过JOption窗格输入的行,这些行的颜色和位置都是随机的。程序构建成功,但它不会生成用户输入的行数,也不会设置随机颜色(这在我的代码底部)。有人能解释一下如何解决这个问题吗?非常感谢。 编辑:修复了曲线大括号,但仍然不起作用。 编辑:除随机颜色外,所有颜色现在都正常工作

import javax.swing.*;   //for JFrame
import java.awt.*;      //for Graphics and Container
import java.util.Random;
import javax.swing.JOptionPane;

// other import statements here


public class RandomGraphics
{
    // constants are used to draw the grid, and for you to put shapes in the grid
    public static final int MIDX = 400;
    public static final int MIDY = 300;
    public static final int MAXX = 799;
    public static final int MAXY = 599;
    public static final int COLOR = (int) (Math.random() * 256);

    // make another constant for the color value that will
    // be used to generate a random color

    public static void main( String[] args )throws InterruptedException
    {
        //*** This next section sets up the graphics window.
        //*** You are not required to understand it
        Container contentPane;
        Graphics g;
        JFrame win = new JFrame("Random Graphics");
        win.setSize(825,650);
        win.setLocation(0,0);
        win.setVisible(true);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = win.getContentPane();
        contentPane.setBackground(Color.white);
        g = contentPane.getGraphics();
        Thread.sleep(50);
        //*** done setting up graphics window


        // Draws Grid - DO NOT CHANGE
        // After you use JOptionPane to get the number of lines, you can move this
        // section of code to just after that, so the lines will not disappear
        g.drawRect(0,0,MAXX+1,MAXY+1);
        g.drawLine(0,MIDY,MAXX,MIDY); // horizontal line
        g.drawLine(MIDX,0,MIDX,MAXY); // vertical line


        // Create Random object
        Random r = new Random();



        // Top left quadrant:


        // Use a JOptionPane to ask the user to enter the number of lines 1 to 100.
        int count = 0;
        do
        {

            String morelines = JOptionPane.showInputDialog("Enter a number of lines between 1 to 100");
            count = Integer.parseInt(morelines);
        }
        while(count > 100 || count < 1);


        for(int i = 1; i >= count; i++)
{
                g.setColor(new Color (r.nextInt(COLOR), r.nextInt(COLOR), r.nextInt(COLOR)));
                g.drawLine(r.nextInt(MIDX), r.nextInt(MIDY), r.nextInt(MIDX), r.nextInt(MIDY));
}
import javax.swing.*//对于JFrame
导入java.awt.*//用于图形和容器
导入java.util.Random;
导入javax.swing.JOptionPane;
//这里还有其他导入语句
公共类随机图形
{
//常数用于绘制网格,并用于在网格中放置形状
公共静态最终int MIDX=400;
公共静态最终int MIDY=300;
公共静态最终整数MAXX=799;
公共静态最终整数MAXY=599;
公共静态最终整数颜色=(int)(Math.random()*256);
//为将显示的颜色值设置另一个常量
//用于生成随机颜色
公共静态void main(字符串[]args)引发InterruptedException
{
//***下一节将设置图形窗口。
//***你不需要理解它
容器内容窗格;
图形g;
JFrame win=新JFrame(“随机图形”);
win.setSize(825650);
win.setLocation(0,0);
win.setVisible(true);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=win.getContentPane();
contentPane.setBackground(颜色:白色);
g=contentPane.getGraphics();
睡眠(50);
//***完成图形窗口的设置
//绘制网格-不更改
//使用JOptionPane获取行数后,可以移动此项
//代码的一部分在那之后,所以行不会消失
g、 drawRect(0,0,MAXX+1,MAXY+1);
g、 抽绳(0,中间,最大,中间);//水平线
g、 抽绳(中线,0,中线,最大);//垂直线
//创建随机对象
随机r=新随机();
//左上象限:
//使用JOptionPane要求用户输入1到100行的数量。
整数计数=0;
做
{
String morelines=JOptionPane.showInputDialog(“输入1到100之间的行数”);
count=Integer.parseInt(更多行);
}
而(计数>100 | |计数<1);
对于(int i=1;i>=count;i++)
{
g、 设置颜色(新颜色(r.nextInt(颜色)、r.nextInt(颜色)、r.nextInt(颜色));
g、 抽绳(r.nextInt(MIDX)、r.nextInt(MIDY)、r.nextInt(MIDX)、r.nextInt(MIDY));
}
查看缩进,您似乎希望
g.setColor(…)
g.drawLine(…)
位于for循环内。您需要将它们括在大括号中
{}
,否则只有紧跟在for循环后的语句才会位于循环内

g = contentPane.getGraphics();
图形
对象不是持久的,程序员需要在被要求时为它们绘制GUI。有关提示,请参阅本教程的


除了“在循环建议周围始终包含花括号”之外,请注意

for(int i = 1; i >= count; i++)
应该是

for(int i = 1; i <= count; i++)

for(int i=1;i)这是始终使用大括号的一个极好的理由,即使对于单行实体也是如此。这样,您就不必考虑您所做的是否正确。是的,我认为您是对的,我设置了大括号,但现在当我输入并运行程序时,我什么也得不到:(为了更快地获得更好的帮助,请发布一条。好的,谢谢你的建议,我会尝试这样做,但是为线条设置随机颜色不起作用,所以我正在尝试解决这个问题。你的语句也起作用,但我很困惑,我不是总是少于计数,所以它只运行一次吗?
for(int i = 1; i <= count; i++)