Практикум по программированию. Основы. Ветвление. Поле сферы

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
Line 13: Line 13:
  
 
     public static void main(String[] args) {
 
     public static void main(String[] args) {
       
+
 
 
         Scanner in = new Scanner(System.in);
 
         Scanner in = new Scanner(System.in);
       
+
 
System.out.println();
+
        System.out.println();
 
         System.out.println("    Расчет напряженности");
 
         System.out.println("    Расчет напряженности");
 
         System.out.println(" электростатического поля сферы");
 
         System.out.println(" электростатического поля сферы");
 
         System.out.println("--------------------------------");
 
         System.out.println("--------------------------------");
  
         System.out.print(" Введите заряд сферы Q: ");  
+
         System.out.print(" Введите заряд сферы: ");
double q = in.nextDouble();
+
        double q = in.nextDouble();
  
         System.out.print(" Введите радиус сферы R: ");  
+
         System.out.print(" Введите радиус сферы: ");
double r = in.nextDouble();
+
        double r = in.nextDouble();
  
         System.out.println(" Расстояние от центра сферы");
+
         System.out.print(" Расстояние от центра сферы до исследуемой точки: ");
        System.out.print(" до исследуемой точки L: ");  
+
        double l = in.nextDouble();
double l = in.nextDouble();
+
  
 
         System.out.println("--------------------------------");
 
         System.out.println("--------------------------------");
       
 
double e;
 
                   
 
if (l >= r) {
 
  
    e = q / (l * l);
+
        double e = l >= r ? q / (l * l) : 0;
  
} else {
+
        System.out.printf(" Напряженность поля E = %.2f в/м\n", e);
 
+
    e = 0;
+
 
+
}
+
 
+
System.out.printf(" Напряженность поля E = %.2E в/м\n", e);
+
 
     }
 
     }
 
}
 
}
Line 68: Line 57:
 
     cout << "--------------------------------\n";
 
     cout << "--------------------------------\n";
 
      
 
      
     cout << " Введите заряд сферы Q: ";
+
     cout << " Введите заряд сферы: ";
 
     cin >> q;
 
     cin >> q;
 
      
 
      
     cout << " Введите радиус сферы R: ";
+
     cout << " Введите радиус сферы: ";
 
     cin >> r;
 
     cin >> r;
 
      
 
      
     cout << " Расстояние от центра сферы\n";
+
     cout << " Расстояние от центра сферы до исследуемой точки: ";
    cout << " до исследуемой точки L: ";
+
 
     cin >> l;
 
     cin >> l;
 
      
 
      
 
     cout << "--------------------------------\n";
 
     cout << "--------------------------------\n";
 
      
 
      
     double e;
+
     double e = l >= r ? q / (l * l) : 0;
   
+
    if (l >= r) {
+
 
+
        e = q / (l * l);
+
 
+
    } else {
+
 
+
        e = 0;
+
 
+
    }
+
 
      
 
      
     printf(" Напряженность поля E = %.2E в/м\n\n", e);
+
     printf(" Напряженность поля E = %.2f в/м\n\n", e);
 
      
 
      
 
     return 0;
 
     return 0;

Revision as of 17:28, 14 March 2016

Назад · Задачи


Contents

Java

import java.util.Scanner;

public class Task17 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.println();
        System.out.println("     Расчет напряженности");
        System.out.println(" электростатического поля сферы");
        System.out.println("--------------------------------");

        System.out.print(" Введите заряд сферы: ");
        double q = in.nextDouble();

        System.out.print(" Введите радиус сферы: ");
        double r = in.nextDouble();

        System.out.print(" Расстояние от центра сферы до исследуемой точки: ");
        double l = in.nextDouble();

        System.out.println("--------------------------------");

        double e = l >= r ? q / (l * l) : 0;

        System.out.printf(" Напряженность поля E = %.2f в/м\n", e);
    }
}


C++

// g++ 4.2

#include <iostream>

using namespace std;

int main() {
    
    double q, r, l;
    
    cout << "\n     Расчет напряженности\n";
    cout << " электростатического поля сферы\n";
    cout << "--------------------------------\n";
    
    cout << " Введите заряд сферы: ";
    cin >> q;
    
    cout << " Введите радиус сферы: ";
    cin >> r;
    
    cout << " Расстояние от центра сферы до исследуемой точки: ";
    cin >> l;
    
    cout << "--------------------------------\n";
    
    double e = l >= r ? q / (l * l) : 0;
    
    printf(" Напряженность поля E = %.2f в/м\n\n", e);
    
    return 0;
}


Pascal



JavaScript




Назад · Задачи

Personal tools
Namespaces

Variants
Actions
Navigation
Tools