这里有很多关于项目欧拉问题的问题,关于找到低于1000的3和5的所有倍数的总和,但我是Java的初学者,我试图制作我自己的程序来试图弄清楚这一点我的代码存在的问题是,它不断给出答案466
.466是这个问题的错误答案,所以我想知道我做错了什么.
这是我的代码.
package sumofmultiples;
public class sumofMultiples{
public static void main(String[] args){
int number = 1; //<- This is the number that we are checking to be a multiple.
int totalNumber= 0; //<- Total number of multiples counted so far.
while (number < 1000){
if ((number % 3) == 0){
totalNumber++;
//The number is a multiple of 3.
} else if ((number % 5) == 0){
totalNumber++;
//The number is a multiple of 5
}
number++; …
Run Code Online (Sandbox Code Playgroud) 所以基本上我有一些我在几天前工作的代码,有点像Paint,它允许你使用鼠标在屏幕上绘制.我偶然发现了这个属性,我意识到这是非常低效的,我想知道是否有更实际的方法来做到这一点.没有任何理由可以提供我的所有代码,但这里是重要的部分
private static void createAndShowGui() {
SimpleDraw mainPanel = new SimpleDraw();
MenuBar.createMenuBar();
JLabel label = new JLabel();
label.setText("Drawing prototype 0.0.1");
// label.setHorizontalTextPosition(JLabel.NORTH);
label.setFont(new Font("Serif", Font.BOLD, 20));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.PAGE_AXIS));
frame.setVisible(true);
frame.setJMenuBar(MenuBar.getMenuBar());
frame.setBackground(Color.WHITE);
frame.add(label);
Run Code Online (Sandbox Code Playgroud)
上面的代码块设置了jframe(窗口)
@Override
public void mouseDragged(MouseEvent e)
{
// These console outputs are just so that I know what is happening
System.out.println("Event: MOUSE_DRAG");
System.out.println(e.getX());
System.out.println(e.getY());
System.out.println(e.getComponent());
System.out.println(e.getWhen());
System.out.println(e.getButton());
MOUSE_X = e.getX() - 5; //-5 so that the cursor represents the center of the square, not …
Run Code Online (Sandbox Code Playgroud)