如何打印系列直到n个术语(下面的描述)? [英] How do I print a series till n terms (description below)?

查看:64
本文介绍了如何打印系列直到n个术语(下面的描述)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请输入以下内容。



一个数字(N1)

一个数字(N2)

编写一个函数,打印系列3n + 2的第一个N1项,它们不是N2的倍数。

约束条件:

0< N1< 100

0< N2< 100



我的尝试:



Take the following as input.

A number (N1)
A number (N2)
Write a function which prints first N1 terms of the series 3n + 2 which are not multiples of N2.
Constraints:
0 < N1 < 100
0 < N2 < 100

What I have tried:

import java.util.Scanner;

public class series {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();
		int x=s.nextInt();
		int sum=0;
		if(n!=0) {
			for (int i=0; i < 100; i++) {
				sum=3*i+2;
			    if (sum % x == 0) {
			        System.out.println(sum);
			    }
			}
			n--;
		}	
	}
}

推荐答案

怎么样:



How about:

import java.util.Scanner;

public class series
{
	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		int N1 = s.nextInt();
		int N2 = s.nextInt();

		if(N1 < 0 || N1 >= 100 || N2 < 0 || N2 >= 100)
		{
			System.out.println("Invalid input.");
			return;
		}

		for(int n = 0; N1 > 0; n++)	
		{
			int value = 3*n+2;
			if (value % N2 != 0)
			{
				System.out.println(value);
				N1--;
			}
		}
	}
}


引用:

如何打印系列直到n个术语(下面的描述)?

How do I print a series till n terms (description below)?



你怎么能写这个代码而无法理解什么是错的。

您应该学习调试器并观察执行的代码。

-----

您的代码行为不符合您的预期,你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的是什么应该这样做,它没有发现错误,只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows /jdb.html [ ^ ]

https: //www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器仅显示您的代码正在执行的操作,您的任务是与什么进行比较应该这样做。


How can you write this code and be unable to understand what is wrong.
You should learn the debugger and watch the code execute.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于如何打印系列直到n个术语(下面的描述)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆