Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help me with functions

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me with functions

    Create a function that calculates the sum of the first 100 natural numbers.
    Create a MaxInt class that allows you to read the largest unique integer between 5 numbers
    Create a function that accepts N ratings (between 0 and 20) and displays the class average
    Create a funciton that reads an integer N and prints all the odd numbers less than N
    Write a prgram that presents the user with a vertical menu with options (USE VARIOUS MENUS)
    1. Insert, 2. Remove, 3. Consult 4. Save and 5 Exit
    Then the program should read an int, which will only be valid if it is between 1 and 5 and present to the user, verbatim the chosen option or the message "Option invalid"
    When choosing an option 5 exit

    Thats code only use functions

    --- Update ---

    package com.company;

    import java.util.Scanner;

    public class Main {


    public static void main(String[] args) {

    // o que eu quero pegar x
    // adicionar o que vem a seguir
    // e resultado final durante x vezes
    /* int soma=0;

    for (int i=0; i<=100; i++)
    soma = i + soma;
    System.out.println(soma);
    /*
    -----------------------------------------------------------------

    Scanner sc = new Scanner (System.in);
    int valor[] = new int[4];
    int maior = 0, menor = 0;

    for(int i = 0; i < valor.length; i++){
    System.out.print("Digite o " +(i+1)+"º valor: ");
    valor[i] = sc.nextInt();
    if(valor[i] > maior){
    maior = valor[i];
    }
    }
    for (int j = 0; j < valor.length; j++) {
    if(valor[j] < menor){
    menor = valor[j];
    }
    }
    System.out.println("Maior valor = "+ maior);
    System.out.println("Menor valor = "+ menor);
    }
    }
    ------------------------------------------------------------------------------------------

    Scanner ent = new Scanner(System.in);
    int nota1, nota2, nota3, nota4, nota5;
    int media, i, medAluno = 0;

    for (i = 0; i <= 20; i++) {

    medAluno++;
    // recebe a 1º nota
    System.out.println("Aluno 1, digite a 1ª nota");
    nota1 = ent.nextInt();

    // recebe a 2º nota
    System.out.println("Aluno 2, digite a 2ª nota");
    nota2 = ent.nextInt();

    // recebe a 3º nota
    System.out.println("Aluno 3, digite a 3ª nota");
    nota3 = ent.nextInt();

    // recebe a 3º nota
    System.out.println("Aluno 4, digite a 4ª nota");
    nota4 = ent.nextInt();

    System.out.println("Aluno 5, digite a 5ª nota");
    nota5 = ent.nextInt();


    // calcula a média
    media = (nota1 + nota2 + nota3 + nota4 + nota5) / 5;
    System.out.println("A média da turma é de: " + media);
    }
    }
    }


    */

    //-----------------------------------------------------------------------------------------------------//
    /*
    // declaração de vetor
    int[] vetor = new int[10];

    // declaração de variáveis
    int i = 0, slot = 0;

    do{
    // validação números ímpares
    if(i % 2 == 1){
    vetor[slot] = i;
    slot++;
    }
    i++;
    }while(slot < 10);

    // zerando o valor de i
    i = 0;

    // imprimindo os números ímpares
    for(; i < vetor.length; i++){
    System.out.println(vetor[i]);
    }

    -------------------------------------------------------------------------------------

    int opcao;
    String nome;
    int i = 0;

    Scanner op = new Scanner(System.in);
    Scanner nm = new Scanner(System.in);

    System.out.println("Menu");
    nome = nm.nextLine();

    do{

    System.out.println("1-Inserir;");
    System.out.println("2-Remover;");
    System.out.println("3-Consultar;");
    System.out.println("4-Gravar");
    System.out.println("5-Sair");
    opcao = op.nextInt();

    switch(opcao){

    case 0:
    System.out.println("Ok, ate mais ...");
    break;

    case 1:
    for(int c = 0;c<1;c++){
    System.out.println(nome);
    }
    break;

    case 2:
    while(i<2){
    System.out.println(nome);
    i++;
    }
    break;
    case 3:

    do{
    System.out.println(nome);
    i++;
    }while(i<3);
    break;

    case 4:

    do{
    System.out.println(nome);
    i++;
    }while(i<2);
    break;

    case 5:

    do{
    System.out.println(nome);
    i++;
    }while(i<5);
    break;
    }
    }while(opcao != 5);

    System.out.println("Programa encerrado.");
    }

    */
    }
    }



    exercícios while-if-case
    package com.company;

    import java.util.Scanner;

    public class Main {

    public static void main (String[] args) {
    /*
    int numero = 20;

    if(numero>0){
    System.out.println ("O número é superior a 0");
    System.out.println ("Metade é "+ numero/2);
    }
    else if{
    System.out.println ("O número é superior a 0");
    System.out.println ("Metade é "+ numero/2);

    else
    System.out.println ("Falso");

    }
    if(numero +5 ==25)
    System.out.println ("Verdadeiro 25");
    else
    System.out.println ("Falso");
    --------------------------------------------------------------------
    int hora = 19;

    if (hora <20)
    System.out.println ("Bom dia");
    else
    System.out.println ("Boa Noite");

    String resultado = (hora <20) ? "Bom Dia" : "Boa Noite";
    System.out.println (resultado);

    ------------------------------------------------------------------------

    int peso = 50;
    switch (peso){
    case 40: System.out.println ("Magro"); break;
    case 70: System.out.println ("Certo"); break;
    case 90: System.out.println ("As meninas da turma"); break;
    case 150: System.out.println ("José, Ladeira e Vicente"); break;
    default: System.out.println("Prof");
    }

    --------------------------------------------------------------------------------

    int i =5;
    while (i<5) {
    System.out.println("While");
    System.out.println(i);
    i++;
    } //end While

    i=5;
    do {
    System.out.println("Do While");
    System.out.println(i);
    i++;
    } while (i<5); // end Do While
    -----------------------------------------------------------------
    for (int i =0; i<50; i++) {
    System.out.println(i);
    if(i==20) break;
    }
    */

    }


    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Please help me with functions

    Do you have any specific java programming questions?
    Note: java uses methods not functions. Are you sure you are writing this in the correct language. C++ uses functions.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    See: http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. array+functions
    By ayisha in forum Loops & Control Statements
    Replies: 7
    Last Post: January 9th, 2018, 08:05 PM
  2. [SOLVED] Deprecated functions
    By dicdic in forum Java Theory & Questions
    Replies: 3
    Last Post: December 15th, 2013, 08:11 PM
  3. Using functions in a calculator
    By aesguitar in forum Java Theory & Questions
    Replies: 9
    Last Post: November 17th, 2013, 10:54 AM
  4. How this GUI functions
    By mathobject in forum Java Programming Tutorials
    Replies: 3
    Last Post: October 15th, 2012, 08:10 AM
  5. Hash Functions
    By Blueshark in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 03:04 PM